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 format()
Python Program to Illustrate Different Set Operations
Python Set difference()
Python tuple()
Python String isalnum()
Python String lstrip()
Python Program to Solve Quadratic Equation
Python Program to Count the Number of Each Vowel
Python String title()
Python pass statement
Python Input, Output and Import
Python del Statement
Python Program to Check the File Size
Python list()
Python Program to Add Two Numbers
Python Program to Create a Long Multiline String
Python String isidentifier()
Python String format()
Python bin()
Python List copy()
Python String replace()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python List reverse()
Python Program to Compute the Power of a Number
Python String encode()
Python String ljust()
Python String isprintable()
Python String rstrip()
Python any()
Python List sort()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Check if a Number is Positive, Negative or 0