How can I generate random integers between 0 and 9 using Python?


In this article, we will show you how to generate random integers between 0 and 9 in Python. Below are the various methods to accomplish this task:

  • Using randint() method

  • Using randrange() method

  • Using secrets module

To generate random integers within certain ranges, Python supports a variety of functions. We will discuss Python's built-in random module for generating random numbers.

The two primary functions provided by the random module to produce random integers are randint() and randrange(). In order to produce random values, a second module called secrets is also used.

Using randint() method

The random.randint() method is used to return a random integer within the specified range.

NOTE

Float numbers cannot be used in the randint() method. If you enter values other than integers, a ValueError: non-integer arg 1 error will be raised.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the random module.

  • Use the randint() function of the random module to generate a random integer between 0 and 9.

  • Print the generated random integer between 0 and 9.

Example

The following program generates a random integer between 0 and 9 using random.randint() method –

# importing random module import random # generating a random integer between 0 and 9 without using a loop randomNumber = random.randint(0, 9) # printing the random integer between 0 and 9 print("The generated random integer between 0 and 9 = ", randomNumber)

Output

On executing, the above program will generate the following output −

The generated random integer between 0 and 9 = 4

Generating some random numbers between 0 and 9 using randint() with for loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the random module.

  • Use the for loop to traverse in the specified range using the range() function(returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number). Here we are running the loop 5 times.

  • Use the randint() function of the random module to generate a random integer between 0 and 9.

  • Print the generated 5 random integers between 0 and 9.

Example

The following program generates some random integers between 0 and 9 using random.randint() method with the for loop –

# importing random module import random print("The 5 generated random integers between 0 and 9 using for loop: ") # traversing in the range from 0 to 5(excluded) # (running the loop 5 times) for i in range(5): # generating a random integer between 0 and 9 randomNumber = random.randint(0, 9) # printing the generated random integer print(randomNumber)

Output

On executing, the above program will generate the following output −

The 5 generated random integers between 0 and 9 using for loop:
7
2
4
8
5

Using randrange() method

The random.randrange() method is used to return a random integer within the specified range.

Syntax

random.randrange(start, stop, step)

Parameters

  • start(optional) - an integer representing the starting position. 0 is the default.

  • stop(required) - an integer representing the ending position

  • step(optional) - an integer representing the incrementation. 1 is the default.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the random module.

  • Use the randrange() function of the random module to generate a random integer between 0 and 9 by passing only the stop argument.

  • Print the generated random integer between 0 and 9.

Example

The following program generates a single random integer between 0 and 9 using random.randrange() method –

# importing random module import random # generating a random integer between 0 and 9 without using loop # we are only passing the stop value here randomNumber = random.randrange(9) # printing the random integer between 0 and 9 print("The generated random integer between 0 and 9 = ", randomNumber)

Output

On executing, the above program will generate the following output −

The generated random integer between 0 and 9 = 2

Generating some random numbers between 0 and 9 using randrange() with for loop

Example

The following program generates some random integers between 0 and 9 using random.randrange() method with for loop –

# importing random module import random print("The 5 generated random integers between 0 and 9 using for loop: ") # traversing in the range from 0 to 5(excuded) # (running the loop 5 times) for i in range(5): # generating random integer between 0 and 9 # we are passing the start, stop parameters here randomNumber = random.randrange(0, 9) # printing the generated random integer print(randomNumber)

Output

On executing, the above program will generate the following output −

The 5 generated random integers between 0 and 9 using for loop:
6
1
7
2
2

Using secrets module

To generate random integers, we can use the randbelow() function found in the secrets module. It generates random numbers with high cryptographic security.

In Python 3.6, the secrets module is a new one. For purposes involving security or cryptography, this is preferable than the random module.

Example

The following program generates 5 random integers between 0 and 9 using randbelow() function with the for loop –

# importing randbelow function from secrets module from secrets import randbelow # traversing in the range from 0 to 5(excluded) using for loop # (running the loop 5 times) for i in range(5): # generating a random integer between 0 and 9 using randbelow() function print(randbelow(9))

Output

On executing, the above program will generate the following output −

0
5
4
4
8

Conclusion

We learned about two different modules for generating random integers between 0 and 9. To generate random integers between 0 and 9, we used two functions from Python's random module, randint() and randrange(). We also looked at the new secrets module, which was introduced in Python 3.x. We also talked about these functions with and without a loop.

Updated on: 28-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements