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 number print ("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.randrange 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)) |
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 number print ("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 number print ("Random selected number is : " + str(random_num)) |
Original list is : [1, 4, 5, 2, 7] Random selected number is : 7
Recommended Posts:
- Python | Remove random element from list
- Generating random number list in Python
- Python | Generate random numbers within a given range and store in a list
- Python | Pandas Series.select()
- Python | Select dictionary with condition given key greater than k
- Random Numbers in Python
- random.seed( ) in Python
- Generating random Id's in Python
- Random Walk (Implementation in Python)
- Generating Random id's using UUID in Python
- Python | random.sample() function
- numpy.random.rand() in Python
- numpy.random.randn() in Python
- Random Forest Regression in Python
- Python | Get random dictionary pair
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



