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 Check Prime Number
Python Program to Check If a String Is a Number (Float)
Python Program to Count the Number of Digits Present In a Number
Python Set issuperset()
Python Program to Print Hello world!
Python max()
Python Program to Remove Punctuations From a String
Python compile()
Python strftime()
Python Statement, Indentation and Comments
Python Program to Find LCM
Python Tuple
Python String split()
Python int()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python String isalnum()
Python round()
How to get current date and time in Python?
Python filter()
Python Program to Display Calendar
Python sum()
Python String translate()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Make a Flattened List from Nested List
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Calculate the Area of a Triangle
Python Operator Overloading
Python Program to Solve Quadratic Equation
Python Matrices and NumPy Arrays
Python String isdecimal()
Python str()
Python Keywords and Identifiers