Python | Select random value from a list
Generating random numbers has always been an useful utility in day-day programming for games or various types of gambling etc. Hence knowledge and shorthands of it in any programming language is always a plus to have.
Let’s discusses all different ways to select random values from a list.
Method #1 : Using random.choice()
This method is designed for the specific purpose of getting random number from the container and hence is the most common method to achieve this task of getting a random number from a list.
# Python3 code to demonstrate # to get random number from list# using random.choice()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 numberprint ("Random selected number is : " + str(random_num)) |
Original list is : [1, 4, 5, 2, 7] Random selected number is : 1
Method #2 : Using random.randrange()
This method is used to generate a random number in a range, for lists, we can specify the range to be 0 to its length, and get the index, and then corresponding value. This also gives the option of getting even placed elements or elements at index of certain multiple.
# Python3 code to demonstrate # to get random number from list# using random.randrangeimport 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 numberprint ("Random selected number is : " + str(random_num)) |
Original list is : [1, 4, 5, 2, 7] Random selected number is : 7
Method #3 : Using random.randint()
Yet another method to generate the random number, this also can be used to generate any number in a range and then using that number index, we can find the value at corresponding index, just like the above-mentioned technique. But it differs by the fact that it required 2 mandatory arguments for range.
# Python3 code to demonstrate # to get random number from list# using random.randint()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 numberprint ("Random selected number is : " + str(random_num)) |
Original list is : [1, 4, 5, 2, 7] Random selected number is : 4
Method #4 : Using random.random()
This method generates the floating point numbers in the range of 0 to 1. We can also get the index value of list using this function by multiplying the result and then typecasting to int to get integer index and then corresponding list value.
# Python3 code to demonstrate # to get random number from list# using random.random()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 numberprint ("Random selected number is : " + str(random_num)) |
Original list is : [1, 4, 5, 2, 7] Random selected number is : 7




