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 Convert Decimal to Binary, Octal and Hexadecimal
Python Matrices and NumPy Arrays
Python Program to Check Whether a String is Palindrome or Not
Python String index()
Python Program to Check If a String Is a Number (Float)
Python Program to Check the File Size
Python Program to Print Hello world!
Python Program to Count the Occurrence of an Item in a List
Python String rindex()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python map()
Python Program to Find Numbers Divisible by Another Number
Python all()
Python complex()
Python round()
Python List append()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python bytes()
Python Program to Check if a Number is Odd or Even
Python hasattr()
Python String join()
Python Program to Get the Full Path of the Current Working Directory
Python datetime
Python Namespace and Scope
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Package
Python Dictionary keys()
Python dict()
Python Numbers, Type Conversion and Mathematics
Python isinstance()
Python setattr()
Python print()