Python Program to generate a random number

February 10, 2022, Learn eTutorial

In this simple python program, we need to generate random numbers. It's a beginner-level python program.

To understand this example, you should have knowledge of the following Python programming topics:

How to generate random numbers in python?

This python program is to generate a random number between a range. For example, if we want to generate a random number between 100 and 500. We have to use this simple python program. Here we use a random module, which is a built-in module in python to generate random numbers between a range of numbers. The Random module has many methods like

  1. seed(): this is used to initialize a random number generator.
  2. getstate(): Used to capture the internal state of the random generator.
  3. setstate(): It is used to set the state of the generator.

And a lot more. Here we can use the method randint() or randrange() which is used to generate the random numbers in the given range. So in this beginner python program, we accept the number range( here we use a predefined number range) and import the module randomly. Finally, print the result using the print(random.randint(100,500))


STEP 1: Import the in-built module called random, used in python to generate a random number.

STEP 2: Use the random module with int to generate the values between some ranges. We can also accept those values from the user.

STEP 3: Print the generated number using the print statement in python.

Python Source Code

                                          import random