In this example, you will learn to generate a random number in Python.
To understand this example, you should have the knowledge of the following Python programming topics:
To generate random number in Python, randint()
function is used. This function is defined in random module.
Source Code
# Program to generate a random number between 0 and 9 # importing the random module import random print(random.randint(0,9))
Output
5
Note that we may get different output because this program generates random number in range 0 and 9. The syntax of this function is:
random.randint(a,b)
This returns a number N in the inclusive range [a,b]
, meaning a <= N <= b
, where the endpoints are included in the range.
Related posts:
Python property()
Python Program to Add Two Numbers
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Java Program to Implement the Program Used in grep/egrep/fgrep
Python help()
Python Dictionary copy()
Python String strip()
Python bytes()
Python Program to Compute the Power of a Number
Python getattr()
APIs in Node.js vs Python - A Comparison
Python complex()
Python strptime()
Python Program to Copy a File
Python Program to Slice Lists
Python Dictionary values()
Python Program to Remove Punctuations From a String
Python Program to Find Hash of File
Python String isdecimal()
Python String casefold()
Python locals()
Python Custom Exceptions
Python Set discard()
Python List append()
Python Program Read a File Line by Line Into a List
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Add Two Matrices
Python Deep Learning Cookbook - Indra den Bakker
Python Package
Python Program to Get File Creation and Modification Date
Python memoryview()