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:
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Deep Learning in Python - LazyProgrammer
Python Dictionary
Python String rindex()
Python locals()
Python Type Conversion and Type Casting
Python format()
Python Dictionary update()
Python Set copy()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Iterate Through Two Lists in Parallel
Python Namespace and Scope
Python String count()
Python Functions
Python String isupper()
Python Program to Extract Extension From the File Name
Python Program to Iterate Over Dictionaries Using for Loop
Python next()
Python List remove()
Python float()
Python if...else Statement
Python Global, Local and Nonlocal variables
Python Data Types
Python Dictionary values()
Python for Loop
Python Set update()
Python Shallow Copy and Deep Copy
Python Variables, Constants and Literals
Python repr()
Python compile()
Python Program to Get the Class Name of an Instance
Python max()