In this post, we are going to learn 5 ways to Generate a list of random numbers in Python. The python random module has three built-in functions randint(),randrange() ,sample() that is used for generate the random number as well as The numpy module np.randint(),np.Uniform() functions.To run all these first we have to install and import numpy into our program.
1. randint() to generate a list of random number
The python module random function randint() is generate a random integer. But to generate a list of random numbers we have to use list comprehension in a given range. In the below example it will generate 5 numbers in a given range from 0 to 15.
#python3 program to Generate list of random numbers
import random
rand_list = [random.randint(0,15) for val in range(5)]
print(rand_list)
Output
[7, 10, 10, 15, 13]
2. randrange() to generate a list of random number
The Python random module randrange() accepts three arguments start, stop, and step. We have generated two lists of random numbers. The steps we have followed to generate random numbers. Let us understand with the below program.
- In the below example We will generate the first list of 5 random numbers in a given range from numbers 0 to 15.
- We are generating the Second list of a random number in which the randrange() accepts three arguments start, stop. steps
- start: Where the generation of random numbers start default is zero
- stop: where the generation of random numbers stops mandatory parameter
- steps: The value is added to the next random number the default value is 1.
#python3 program to Generate list of random number
import random
rand_list = [random.randrange(0,15) for val in range(5)]
print(rand_list)
import random
rand_list = [random.randrange(3,15,3) for val in range(5)]
print('List of random with start,stop,step:\n'rand_list)
Output
[0, 5, 6, 3, 5]
List of random with start,stop,step:
[6, 3, 12, 6, 12]
3. sample() to generate a list of random number
The random module sample() function is also used to generate random numbers. It accepts two arguments the range() to generate a list of random numbers and the number of elements that will be generated in a given range. We are using the sample() function to generate 5 numbers in a given range from 0 to 15.
Syntax
random.sample(range, count)
import random
rand_list = random.sample(range(0,15), 5)
print(rand_list)
Output
[6, 5, 14, 13, 1]
4. np.randint() to generate a list of random number
The numpy randint() function is used to create a numpy array of a given size and fill it with random integer values from low to high. In this example, we are creating three different arrays.
- First, we have created a list of the random numbers (npLst) in the range 5 mean 0 to 4, without explicitly specifying the size.
- Secondly, We have created a nested list of random numbers(npNestedList) of range -9 to 9 of size(3,3)
import numpy as np
npLst = np.random.randint((3,3))
npNestedList = np.random.randint(-9,9,size=(3,3))
print('Array1:',npLst )
print('\nArray2:\n',npNestedList )
Output
Array1: 2
Array2:
[[-9 -1 7]
[-3 7 -3]
[ 4 7 -3]]
5.np.Uniform() function to create numpy array float range
The numpy uniform() function takes three arguments the low, high, and size, and creates an array that values are filled based on uniform distribution over the half-open interval. In this example, we are creating an array that has low =0.5,high=13.3, and size(3,3)
import numpy as np
lst= np.random.uniform(low=0.5, high=13.3, size=(3,3))
print('List:',lst)
Output
List: [[ 8.44141064 2.2643609 7.97413926]
[ 9.06224892 2.36153455 10.33275063]
[ 1.83125795 12.3641906 8.12921322]]
Summary
In this post, we have learned 5 ways to Generate a list of random numbers in Python by using two modules random and the NumPy module. We have used random.randint(),random.randrange() ,random.sample() and NumPy module np.random.uniform, np.randint() with python programs examples.