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 Matrices and NumPy Arrays
Python exec()
Python Program to Check If a List is Empty
Python Program to Access Index of a List Using for Loop
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Transpose a Matrix
Python String isalpha()
Python time Module
Python Program to Display the multiplication Table
Python Program to Sort a Dictionary by Value
Python iter()
Python Global, Local and Nonlocal variables
Python Program to Count the Number of Each Vowel
Python *args and **kwargs
Python List sort()
Python Program to Illustrate Different Set Operations
Python String splitlines()
Python Set symmetric_difference_update()
Python divmod()
Python vars()
Python Program to Shuffle Deck of Cards
Deep Learning with Python - Francois Cholletf
Python Program to Copy a File
Python map()
Python Set isdisjoint()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python delattr()
Python String title()
Python filter()
Deep Learning with Python - Francois Chollet
Python Program to Display Calendar
Python bin()