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 time Module
Python hasattr()
Python Set symmetric_difference_update()
Python Program to Access Index of a List Using for Loop
Python Program to Convert Kilometers to Miles
Python Dictionary
Python set()
Python format()
Python bytearray()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python len()
Python String isdecimal()
Python Program to Find Numbers Divisible by Another Number
Python if...else Statement
Python repr()
Python String casefold()
Python Program to Display Calendar
Python Program to Find LCM
Python Set issuperset()
Python hash()
Python datetime
Python List copy()
Python Program to Safely Create a Nested Directory
Python sorted()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python String isspace()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Illustrate Different Set Operations
Python String index()
Python String isidentifier()
Python Dictionary fromkeys()
Python filter()