Generate random list in python

Sometimes, in making programs for gaming or gambling, we come across the task of creating a list all with random numbers in Python. This task is to perform in general using loop and appending the random numbers one by one. But there is always a requirement to perform this in the most concise manner. Let’s discuss certain ways in which this can be done.

Random Number Using random module

Python Random module is an in-built module of Python which is used to generate random numbers. This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc.

Method 1: Using the random.randint()

By using random.randint() we can add random numbers into a list.

Python3

import random

rand_list=[]

n=10

for i in range(n):

    rand_list.append(random.randint(3,9))

print(rand_list)

Output

[9, 3, 3, 6, 8, 5, 4, 6, 3, 7]

Method 2: Using random.sample() 

This single utility function performs the exact required as asked by the problem statement, it generated N no. of random numbers in a list in the specified range and returns the required list.

Python3

import random

res = random.sample(range(1, 50), 7)

print ("Random number list is : " +  str(res))

Output

Random number list is : [49, 20, 23, 34, 6, 29, 35]

Method 3: Using list comprehension + randrange() 

The naive method to perform this particular task can be shortened using list comprehension. randrange function is used to perform the task of generating the random numbers. 

Python3

import random

res = [random.randrange(1, 50, 1) for i in range(7)]

print ("Random number list is : " +  str(res))

Output

Random number list is : [32, 16, 9, 28, 19, 31, 21]

Method 4: using loop + randint()

Python3

import random

lis = []

for _ in range(10):

    lis.append(random.randint(0, 51))

print(lis)

Output:

[3, 11, 48, 2, 48, 2, 8, 51, 8, 5]

Random Number Using Numpy

The random function provided by the Numpy module can be more useful for you as it provides little better functionality and performance as compared to the random module.

Method 1: Generating a list of random integers using numpy.random.randint function

This function returns random integers from the “discrete uniform” distribution of the integer data type.

Python3

import numpy as np

print(list(np.random.randint(low = 3,high=8,size=10)))

print(list(np.random.randint(low = 3,size=5)))

Output: 
[5, 3, 6, 7, 4, 5, 7, 7, 7, 7]
[0, 2, 1, 2, 1]

Method 2. Generating a list of random floating values using numpy.random.random_sample function

This function return random float values in half open interval [0.0, 1.0).

Python3

import numpy as np

print(np.random.random_sample(size = 4))

print(np.random.random_sample(size = (4,4)))

output:
[0.08035145 0.94966245 0.92860366 0.22102797]
[[0.02937499 0.50073572 0.58278742 0.02577903]
 [0.37892104 0.60267882 0.33774815 0.28425059]
 [0.57086088 0.07445422 0.86236614 0.33505317]
 [0.83514508 0.82818536 0.1917555  0.76293027]]

The benefit of using numpy.random over the random module of Python is that it provides a few extra probability distributions which can help in scientific research.


How do you randomly generate a list in Python?

randrange() to select random value from a list. random. randrange() method is used to generate a random number in a given range, we can specify the range to be 0 to the length of the list, and get the index, and then the corresponding value.

How do you generate a random number in a range in Python?

Use randrnage() to generate random integer within a range Use a random. randrange() function to get a random integer number from the given exclusive range by specifying the increment. For example, random. randrange(0, 10, 2) will return any random number between 0 and 20 (like 0, 2, 4, 6, 8).

What is random () in Python?

Python Random module is an in-built module of Python which is used to generate random numbers. These are pseudo-random numbers means these are not truly random. This module can be used to perform random actions such as generating random numbers, print random a value for a list or string, etc.

How do you shuffle a list value in Python?

You can shuffle a list in place with random. shuffle() .