Register Login

Generate Random Integers in Python

Updated Jul 01, 2022

In Python, there are different methods to generate random numbers. One of these categories is to generate random integers ranging from 0 and 9. For this, users need to use some built-in functions that Python provides, including some custom code.

We often require random numbers to create lottery games, apps, casino games, number games, cryptographic programs, etc. Among random numbers, integer generating random numbers are the most popular. In this article, users will learn all the different processes to generate random integers.

How to generate Random Integers?

In between some specific ranges, Python supports distinct functions for generating random integers. This article brings three methods of generating random integers between 0 to 9. The random module renders two primary built-in functions in Python to generate random integers, namely randint() and randrange().

In addition to these two functions, the article adds module secrets, which users can use to generate integers ranging from 0 to 9.  

Using the randrange() Function:

Python users can use this function to generate random integers. The function belongs to a random module that accepts three parameters, start, stop and step.

Syntax:

random.randrange(stop)
random.randrange(start, stop, step)

Code:

import random
print("Using no loop ")
a = random.randrange(9)
print("Generate random integers from range 0 and 9: ", a)
print("Using loop ")
print("Generate random integers from range 0 and 9: ")
for int in range(9):
     b = random.randrange(9)
     print(b)

Output:

Using the randint() Function:

Another function in Python to generate random Integers is using the randint() Function. This function also belongs to random modules and returns a random integer within the range 0 to 9.

Syntax:

random.randint(a,b)

Code Snippet:

import random
print("Using no loop")
a = random.randint(0, 9)
print("Generate random integers from range 0 and 9: ", a)
print("Using loop")
print("Generate random integers from range 0 and 9: ")
for int in range(9):
    b = random.randint(0, 9)
    print(b) 

Output:

Using the Secret Module:

The last method to generate random integers from 0 to 9 is the randbelow() function from the secrets module. It is the latest module in Python 3.6. The function generates strong random numbers cryptographically. Users can use this module to generate random numbers for managing crucial data.

These can be account authentication, passwords, security tokens, and related secrets. This module is better than the two methods mentioned above, randrange() and randint(), for cryptography or security uses.

Code Snippet:

from secrets import randbelow
for _ in range(9):
    print(randbelow(9))

Output:

Conclusion:

This article has catered to two different modules for generating random integers ranging from 0 to 9. The two modules random module and the secrets module bring functions, namely randrange(), randint(), and randbelow() functions in Python.

The code snippets include the method of generating random numbers with or without a loop. Among both of these, the random is the most efficient and prominent module used for generating random numbers.


×