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 sorted()
Python exec()
Python String isalnum()
Python Program to Display the multiplication Table
Python Program to Differentiate Between type() and isinstance()
Python Directory and Files Management
Python Multiple Inheritance
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python String endswith()
Python String isnumeric()
Python Program to Swap Two Variables
Python Program to Create Pyramid Patterns
Python Exception Handling Using try, except and finally statement
Python locals()
Python Custom Exceptions
Python List clear()
Python Function Arguments
Python Type Conversion and Type Casting
Python Data Types
Python Program to Append to a File
Python strptime()
Python String center()
Python String isspace()
Python Program to Merge Mails
Python String rsplit()
Python if...else Statement
Python Program to Compute the Power of a Number
Python Get Current time
Python Dictionary clear()
Python dict()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python String isdigit()