Signup/Sign In

How to Generate random integers between 0 and 9

In this article, we will learn to generate random integers between 0 and 9 in Python. We will use some built-in functions available in Python and some custom code as well. Let us look at different ways to generate random numbers.

Generate Random integers

Python supports different functions to generate random integers between specific ranges. We will discuss the built-in random module to generate random numbers in Python. The random module provides two main functions to generate random integers as randint() and randrange(). We will also look at how float values are handled by random module. An additional module secrets is also used here to generate random values.

Example: Using the randint() Function

This function belongs to random module and it generates random integers between 0 and 9. This function returns a random integer within a range.

Note: You cannot use float number in randint(). It will raise a ValueError: non-integer arg 1 if you use values other than an integer.

import random

print("Without loop")
x = random.randint(0, 9)
print("Random integer between 0 and 9: ", x)

print("Using loop")
print("Random integers between 0 and 9: ")
for i in range(9):
    y = random.randint(0, 9)
    print(y)


Without loop
Random integer between 0 and 9: 1
Using loop
Random integers between 0 and 9:
5
1
7
8
4
9
9
3
3

Example: Using the randrange() Function

This function belongs to random module and it generates random integers between 0 and 9. The randrange() accepts three parameters - start, stop, and step. This function returns a random integer within a range. The higher limit of range is not included.

Note: You cannot use float value randrange(). It will raise a ValueError: non-integer arg 1 for randrange() if you used values other than an integer.

import random

print("Without loop ")
x = random.randrange(9)
print("Random integer between 0 and 9: ", x)

print("Inside loop ")
print("Random integers between 0 and 9: ")
for i in range(9):
     y = random.randrange(9)
     print(y)


Without loop
Random integer between 0 and 9: 5
Inside loop
Random integers between 0 and 9:
6
3
5
8
3
3
0
3
8

Example: Using secrets module

We can use randbelow() function from secrets module to generate random integers. It generates cryptographically strong random numbers. The secrets module is new in Python 3.6. This is better than the random module for cryptography or security uses. The below example uses randbelow() to randomly print integers in the inclusive range of 0-9.

from secrets import randbelow

for _ in range(9):
    print(randbelow(9))


6
8
4
3
2
6
8
1
3

Conclusion

In this article, we learned about two different modules to generate random integers between 0 and 9. We used two functions such as randint() and randrange() from random module in Python to generate random integers between 0 and 9. We also studied a new secrets module that is introduced in version Python 3.x. We also discussed these functions using a loop and without a loop. A small part of the article introduced random.uniform() to generate random float values.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.