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 Namespace and Scope
Machine Learning with Python for everyone - Mark E.Fenner
Python del Statement
Python type()
Python Dictionary fromkeys()
Python Program to Extract Extension From the File Name
Python List copy()
Deep Learning with Python - Francois Chollet
Python Program to Find Armstrong Number in an Interval
Python Program to Trim Whitespace From a String
Python break and continue
Python property()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python String strip()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Anonymous / Lambda Function
Python dir()
Python Tuple index()
Python String isnumeric()
Python print()
Python Directory and Files Management
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Sort a Dictionary by Value
Python Program to Check the File Size
Python Dictionary get()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Dictionary copy()
Python Program to Copy a File
Python Set intersection_update()
Python Program to Solve Quadratic Equation
Python Program to Append to a File
Python Variables, Constants and Literals