Open In App

Python | Select random value from a list

Given a list and our task is to randomly select elements from the list in Python using various functions. Selecting random numbers from a list can be used sometimes while building games, choosing a random range, etc. 

Example



Input: [2, 3, 4 , 5, 6 ]
Output: 2
Explaination:The output we are getting is a random value from the input list

Select the Random Value from a List

Below are the methods that we will cover in this article:

Select a random value from a list using random.choice()

This random.choice() function is designed for getting a Random sampling from a list in Python and hence is the most common method to achieve this task of fetching a random number from a list






import random
 
# initializing list
test_list = [1, 4, 5, 2, 7]
 
# printing original list
print("Original list is : " + str(test_list))
 
# using random.choice() to
# get a random number
random_num = random.choice(test_list)
 
# printing random number
print("Random selected number is : " + str(random_num))

Output:

Original list is : [1, 4, 5, 2, 7]
Random selected number is : 1

Select a random value from a list using random.randrange()

The 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.




import random
 
# initializing list
test_list = [1, 4, 5, 2, 7]
 
# printing original list
print("Original list is : " + str(test_list))
 
# using random.randrange() to
# get a random number
rand_idx = random.randrange(len(test_list))
random_num = test_list[rand_idx]
 
# printing random number
print("Random selected number is : " + str(random_num))

Output:

Original list is : [1, 4, 5, 2, 7]
Random selected number is : 7

Select the random value from a list using random.randint()

The random.randint() is used to generate the random number, also this can be used to generate any number in a range, and then using that number, we can find the value at the corresponding index, just like the above-mentioned technique. But it differs by the fact that it requires 2 mandatory arguments for range. 




import random
 
# initializing list
test_list = [1, 4, 5, 2, 7]
 
# printing original list
print("Original list is : " + str(test_list))
 
# using random.randint() to
# get a random number
rand_idx = random.randint(0, len(test_list)-1)
random_num = test_list[rand_idx]
 
# printing random number
print("Random selected number is : " + str(random_num))

Output:

Original list is : [1, 4, 5, 2, 7]
Random selected number is : 4

Select the random value from a list using random.random()

The random.random() method generates the floating-point numbers in the range of 0 to 1. We can also get the index value of a list using this function by multiplying the result and then typecasting it to an integer so as to get the integer index and then the corresponding list value. 




import random
 
# initializing list
test_list = [1, 4, 5, 2, 7]
 
# printing original list
print("Original list is : " + str(test_list))
 
# using random.random() to
# get a random number
rand_idx = int(random.random() * len(test_list))
random_num = test_list[rand_idx]
 
# printing random number
print("Random selected number is : " + str(random_num))

Output:

Original list is : [1, 4, 5, 2, 7]
Random selected number is : 7

Select the random value from a list using random.sample()

Python has a built-in function called random.sample().The random module contains the random.sample() function. It has the ability to choose multiple items from a list.




import random
test_list = [1, 4, 5, 2, 7]
print("Original list is : " + str(test_list))
 
print("Random element is :", random.sample(test_list, 5))

Output:

Original list is : [1, 4, 5, 2, 7]
Random element is : [7, 4, 1, 5, 2]

Select the random value from a list using random.choices()

The random.choices function is stored in the random module(). Selecting numerous items from a list or a single item from a particular sequence is handy with the help of random.choices function.




import random
test_list = [11, 44, 55, 22, 77]
print("Original list is : " + str(test_list))
 
print("Random element is :", random.choices(test_list, k=4))

Output:

the t is : [11, 44, 55, 22, 77]
Random element is : [11, 11, 44, 77]

Select the random value from a list using numpy.random.choice()

The numpy.random.choice() method is used for getting a random sample from an array in numpy. It is also possible to generate a random sample from a list by converting the list to a numpy array.
Import numpy and initialize the list. Convert the list to a numpy array. Use numpy.random.choice() method to select a random value from the array. Print the selected value.

Note: Install numpy using “pip install numpy”




import numpy as np
 
# Initializing list
test_list = [2, 3, 4, 5, 6]
 
# Converting list to numpy array
test_array = np.array(test_list)
 
# Using numpy.random.choice() to get a random number
random_num = np.random.choice(test_array)
 
# Printing the random number
print("Random selected number is : " + str(random_num))

Output:

Random selected number is : 5

Time Complexity: The time complexity of the numpy.random.choice() method is O(k), where k is the size of the sample to be generated.
Space Complexity: The space complexity of the numpy.random.choice() method is O(n), where n is the size of the array.

Select k random value from a list

Here we have grouped all elements in a pair of sizes k.




import random
 
def select_random_Ns(l, k):
    random.shuffle(l)
    res = []
    for i in range(0, len(l), k):
        res.append(l[i:i + k])
    return res
         
l = ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
 
print(select_random_Ns(l, 3))

Output:

[['G', 'G', 'R'], ['K', 'K', 'E'], ['O', 'F', 'E'], ['S', 'E', 'S'], ['E']]

Article Tags :