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 decorator
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Modules
Python Program to Split a List Into Evenly Sized Chunks
Python String isspace()
Python Program to Display the multiplication Table
Python Program to Parse a String to a Float or Int
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Get the Class Name of an Instance
Deep Learning with Python - Francois Cholletf
Python String isdigit()
Python Dictionary update()
Python Dictionary values()
Python Program to Delete an Element From a Dictionary
Python Dictionary clear()
Python String splitlines()
Python Set issuperset()
Python Program to Check if a Number is Positive, Negative or 0
Python Objects and Classes
Python String partition()
Python Exception Handling Using try, except and finally statement
Python property()
Python Global Keyword
Python Global, Local and Nonlocal variables
Python bytearray()
Python ord()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python str()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python frozenset()
Python Multiple Inheritance
Python String isprintable()