Python has several built-in modules that provide different functionalities. One such module is random that supports multiple functions and methods to generate random numbers. Random numbers are used in guessing games, lotteries, etc.

This guide will discuss in detail how to generate random numbers in Python.

How To Generate Random Integers?

There are numerous methods to generate random numbers in Python. Some of them are listed below

  • Using randint()
  • Using randrange() 
  • Using choice()
  • Using Secrets Module

Let’s discuss them one by one.

Method 1: Generate Random Numbers Using randint()

We can use the randint() method to generate random integers. This method only generates random integers so if a float number is provided, the code will throw an error.

Let’s do an example:

import random
RandomNumber = random.randint(0,9)
print("The Random number is ", RandomNumber)

In the above code:

  • The random module is imported.
  • The randint() method is used to generate a random number between 0 to 9.
  • The random number is displayed using the print method.

The output of the above code is as follows:

It can be seen that a random number 9 is generated.

Let’s see an example of generating random numbers between 0 to 9 using a for loop.

Consider the following example:

import random
for i in range(5):
    RandomNumber = random.randint(0,9)
    print("The Random number is ", RandomNumber)

In the above code, a for loop is used to iterate through the given range of numbers and the randint() method is used to generate random numbers.

The output of the above code is as follows:

It can be seen that 5 random integers are printed on the screen.

Method 2: Generate Random Numbers Using randrange()

Another method randrange() is used to generate random integers between the specified range

For instance:

import random
RandomNumber = random.randrange(9)
print("The Random number is ", RandomNumber)

In the above code, all the steps are the same as the previous except for the randrange() method which prints random numbers within the given range.

The output of the above code is as follows:

The random number is displayed on the screen.

Method 3: Generate Random Numbers Using choice()

random.choice() provides a single random integer from a specified range

Let’s see an example:

import random
RandomNumber = random.choice(range(9))
print("The Random number is ", RandomNumber)

In the above code random.choice() method is used to generate a random number between 0 to 9.

The output of the above code is as follows:

It can be seen that a random number 6 is printed on the screen.

Method 4: Generate Random Numbers Using Secrets Module 

The Secrets module has a function called randbelow() to generate random numbers within the given range. This method is commonly used for security and cryptographical purposes.

Let’s do an example:

from secrets import randbelow
for i in range(5):
  print(randbelow(9), end = " ")

In the above code:

  • The randbelow function is imported from the secrets module.
  • A for loop is used to iterate through the given range of numbers
  • The randbelow() method is used to print random integers.

The output of the above code is as follows:

It can be seen that 5 random integers are printed on the screen.

Conclusion

Python has a  module that supports multiple functions and methods to generate random numbers known as random. We can generate random numbers in Python using randint(),randrange(), choice() methods, and secrets module. All these methods generate random numbers within the specified range. In this guide, different methods of generating random numbers in Python have been discussed.