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 Program to Split a List Into Evenly Sized Chunks
Python Set update()
Python String isupper()
Python callable()
Python setattr()
Python Program to Shuffle Deck of Cards
Python Program to Find Factorial of Number Using Recursion
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python del Statement
Python Program to Get File Creation and Modification Date
Python Type Conversion and Type Casting
Python String capitalize()
Python *args and **kwargs
Python iter()
Python pow()
Python if...else Statement
Python Dictionary copy()
Python bytes()
Python String partition()
Python Errors and Built-in Exceptions
Python Program to Check If a List is Empty
Python list()
Python range()
Python len()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Check if a Key is Already Present in a Dictionary
How to Get Started With Python?
Python String isidentifier()
Python String zfill()
Python Program to Measure the Elapsed Time in Python
Python ord()
Python String ljust()