Python Program to Generate a Random Number

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.


Also Read:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community