Python provides us with the random module to generate random numbers in our programs. In this article, we will discuss different ways to create a random number in a given range in python.
Random Number in a Range Using the randint() Function
To create a random number in a range, we can use the randint()
function. The randint()
function takes the lower limit and upper limit of the range as the first and second input arguments respectively. After execution, it returns a random number between the lower limit and the upper limit. Here, the lower limit and upper limit are inclusive and the newly generated random number can be equal to the upper limit or the lower limit. You can create a random number in a range using the randint()
function as follows.
import random
upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
number = random.randint(lower_limit, upper_limit)
print("The random number is:", number)
Output:
The lower limit of the range is: 0
The upper limit of the range is: 1000
The random number is: 739
Random Number in a Range Using the choice() Function
Instead of using the randint()
function, we can use the choice()
function defined in the random module to create a random number. For this, we will first create a sequence of numbers using the range()
function. The range()
function takes the lower limit of the sequence as its first argument, the upper limit of the sequence as its second argument, and the difference between two consecutive numbers in the sequence as the third optional argument. After execution it returns a list containing the sequence of the numbers.
After creating the list, we will pass it as an input argument to the choice()
function. The choice function takes the list as its input argument and returns a random number from the list. In this way, we can create a random number in a given range as shown below.
import random
upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
range_of_nums = range(lower_limit, upper_limit)
number = random.choice(range_of_nums)
print("The random number is:", number)
Output:
The lower limit of the range is: 0
The upper limit of the range is: 1000
The random number is: 848
To obtain more than one random number at a time, you can use the choices()
function. The choices()
function works exactly the same as the choice()
function. Additionally, it takes the count of required random numbers as the second input argument and returns a list of specified random numbers as follows.
import random
upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
range_of_nums = range(lower_limit, upper_limit)
numbers = random.choices(range_of_nums, k=3)
print("The three random numbers are:", numbers)
Output:
The lower limit of the range is: 0
The upper limit of the range is: 1000
The three random numbers are: [105, 858, 971]
Using Numpy Module
We can also generate random numbers in a range using the functions defined in the numpy module. For instance, you can use the numpy.random.randint()
function instead of the random.randint()
function to generate a random number as follows.
import numpy
upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
number = numpy.random.randint(lower_limit, upper_limit)
print("The random number is:", number)
Output:
The lower limit of the range is: 0
The upper limit of the range is: 1000
The random number is: 144
Similarly, you can use the numpy.random.choice()
function to generate random numbers in a range as follows.
import numpy
upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
range_of_nums = range(lower_limit, upper_limit)
number = numpy.random.choice(range_of_nums)
print("The random number is:", number)
Output:
The lower limit of the range is: 0
The upper limit of the range is: 1000
The random number is: 264
Concussion
In this article, we have discussed different ways to generate a random number in a range in python. We have also discussed how to create a list of numbers. To know more about lists in python, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.