In this Python tutorial, we will learn how to generate random integers between 0 and 9 in Python?
Table Of Contents
 Generate random numbers using random.randrange()
 Generate random numbers using random.randint()
 Generate random numbers using numpy.random.randint()
 Generate random numbers using random.uniform()
 Generate Random Numbers using secrets.randbelow()
 Generate random number using numpy.random.choice()
 Generate random numbers using random.sample()
 Summary
Let’s dive into the tutorial.
Generate random numbers using random.randrange()
The randrange() method is available in the random python module, which returns a random number within the given range.
Syntax:
random.randrange(start,stop,step)
Parameters:
 start refers to the starting number. here it is 0.
 stop refers to the ending number. here it is 9.
 step will take an integer, which is used to skip the value between the given ranges.
Example:
In this example, we will generate a random number between 0 and 9.
from random import randrange # Get random number in range 09 num = randrange(0,9) print(num)
Output:
3
In the given range 0 to 9, number 3 is the random number returned.
Generate random numbers using random.randint()
The randint() method is available in the random Python module. It returns a random integer within the given range.
Syntax:
random.randint(start,stop)
Parameters:
 start refers to the starting number. here it is 0.
 stop refers to the ending number. here it is 9.
Example:
In this example, we will generate a random number in between 0 and 9.
from random import randint # Get random number in range 09 num = randint(0,9) print(num)
Output:
7
In the given range 09, number 7 is the random integer returned.
Generate random numbers using numpy.random.randint()
Suppose, if we want to return multiple random integers in the range of 09, then we can use the numpy module. It supports randint() and will return random integers in an array[] separated by a comma.
Syntax:
numpy.random.randint(low, high, size=(n))
It will take three parameters.
 low refers to the starting range. here it is 0
 high refers to the ending range. here it is 9.
 size refers to the number of random integers returned within the given range.
Example:
In this example, we will generate 10 random numbers between 0 and 9.
import numpy # Get 10 random integers from 0 to 9 numbers = numpy.random.randint(low=0, high=9, size=(10)) print(numbers)
Output:
[5 0 3 0 7 1 5 4 2 6]
Generate random numbers using random.uniform()
The uniform() method is available in the random module, which will return a random float number within the given range. We have to typecast from float to integer using int().
Syntax:
random.int(uniform(low,high))
Parameters:
 low refers to the starting number. here it is 0.
 high refers to the ending number. here it is 9.
Example:
In this example, we will generate a random number in between 0 and 9.
from random import uniform # Get random number num = int(uniform(0, 9)) print(num)
Output:
3
In the given range 09, number 3 is the random integer returned.
Suppose, if we want to return multiple random integers in the range of 09, then we can use the numpy module. It supports uniform and will return random integers in an array, separated by a comma. Finally, we are typecasting from float to integer using the astype(int) method.
Syntax:
numpy.random.uniform(low, high, size=(n)).astype(int)
It will take three parameters.
 low refers to the starting range. here it is 0
 high refers to the ending range. here it is 9.
 size refers to the number of random integers returned within the given range.
Example:
In this example, we will generate 10 random numbers in between 0 and 9.
import numpy # Generate 10 random integers from 0 to 9 numbers = numpy.random.uniform(low=0, high=9, size=(10)).astype(int) print(numbers)
Output:
[3 6 2 2 3 4 4 3 8 8]
Generate Random Numbers using secrets.randbelow()
The randbelow() method is available in the secrets module. It returns a random number within the given range.
Syntax:
secrets.randbelow(value)
Parameters:
 value is the maximum range.
Example:
In this example, we will generate a random number within range.
from secrets import randbelow # Generate random number num = randbelow(9) print(num)
Output:
5
In the range 0 to 9, the number 5 is the random number returned.
Generate random number using numpy.random.choice()
The method numpy.random.choice() from numpy module is used to get n random numbers with in the range(value).
Syntax:
numpy.random.choice(value, size )
Parameters:
 value is the maximum range.
 size refers to the number of random integers returned within the given range(value).
Example:
In this example, we will generate 10 random numbers in the range of 0 to 9.
import numpy # Get 10 random integers numbers = numpy.random.choice(10, size=10 ) print(numbers)
Output:
[3 3 5 5 0 5 9 4 2 4]
Generate random numbers using random.sample()
The sample() method available in random python module. It is used to return the random numbers within the given range.
Syntax:
sample(range(start,stop), k)
It will take two parameters.
 range() will take two values
 start refers to the start of range. Here it is 0.
 stop refers to the end of range. Here it is 10.
 k refers to the number of random integers returned within the given range.
Example:
In this example, we will generate 10 random numbers in between 0 and 9.
from random import sample # Get 10 random integers numbers = sample(range(0,10), k=10) print(numbers)
Output:
[6, 7, 4, 3, 1, 5, 8, 2, 0, 9]
Summary
From the above article, we learned six different methods to generate random numbers in python. Using these methods, we can also use the numpy module to generate random numbers. Based on your requirement, you can use any of the above methods. Happy Learning.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.