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 globals()
Python repr()
Python List copy()
Python Program to Represent enum
Python Program to Display Calendar
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python print()
Python String lstrip()
Python Set discard()
Python hasattr()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Deep Learning in Python - LazyProgrammer
Python Program to Find the Sum of Natural Numbers
Python Program to Find LCM
Python Inheritance
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python bool()
Python Anonymous / Lambda Function
Python sum()
Python Program to Get Line Count of a File
Python len()
Python Iterators
Python Program to Solve Quadratic Equation
Python Program to Capitalize the First Character of a String
Python Matrices and NumPy Arrays
Python Multiple Inheritance
Python Program to Find the Size (Resolution) of a Image
Python Program to Reverse a Number
Python Program to Create a Countdown Timer
Python Dictionary keys()
Python Dictionary popitem()