Python

Randomly Select From a Python List

Creating random numbers has always been a valuable utility in our languages for games or different types of betting. In this tutorial, we will deliberate how to choose the random value from the Python list. We will elaborate on it using various methods and techniques. However, we can access random numbers via a random module. Python provides a built-in random module that is valuable to get random elements in a set, list, or tuple. This module is beneficial to perform various tasks such as choosing random numbers and scuffling the numbers. The module holds various functions, such as random.randrange(), and random.choice().

Now, let’s deliberate on all possible ways to choose random elements from a list. Using the Spyder compiler in Windows 10, we will implement all operations of random functions.

Example 1:

In our first method, we use the random.choice()function. This approach is used for the specific purpose of accessing random elements from the ampule. It is the most commonly used process to complete the task of fetching a random item from a Python list. To work with Python’s random functions, we need to open a Spyder IDE and then, create a new blank file through which we implement our program code.

We have created a new file and written our first program using random.choice() function. In this program, we first import a random module and then, create and initialize the list of integers. Next, we use a print function that prints our newly created list. We can then use a random.choice() function, which traverses over the list and selects a random number. Then, we use a print function that displays the output of a selected random number:

import random

my_list = [2, 4, 6, 8, 7]

print (“Original list is: “ +str(my_list))

random_integer = random.choice(my_list)

print (“Random selected integer is :” +str(Random_integer))

Once done, save the file by specifying its name with the “.py” extension.

Once you successfully save your program file, run the code and check how it works:

Example 2:

In our second method, we use the random.randrange() function. It generates a random element in order. For lists, we can specify the range length to be 0, and access the index and its equivalent value. Also, it provides the option of fetching even positioned elements or index elements at the index of a few multiple. To work with Python random.randrange(), we use the same code file “SelectRandomNum.py”.

In this program, we first import a random module. Then, create and initialize a list of integers. Next, we use a print function that prints our newly created list. We can use a random.randrange() function and pass the list into its length. We assign this function to an index variable. After this, we use our list, which takes the index variable and assigns it to another variable:

import random

my_list = [2, 4, 6, 8, 7]

print (“Original list is: “ +str(my_list))

random_index = random.randrasnge(len(my_list))

random_num = my_list[random_index]

print (“Random selected integer is :” +str(Random_num))

Save (Ctrl+S) and run (F5) the “SelectRandonNum.py” program file to check how the random.randrange() function works:

Example 3:

In our third method, we use the random.randint() function. It can also generate a random element in a range. However, the difference is it uses the number index to find the number at the corresponding index. To work with Python random.randint(), we use the same code file “SelectRandomNum.py”.

In this program, we first import a random module and then, create and initialize a list of integers. Next, we use a print function that prints our newly created list. We can use a random.randint() function. The function uses two mandatory parameters for range. We assign this function to an index variable. After this, we use our list that takes the index variable:

import random

my_list = [ 2, 4, 6, 8, 7]

print (“Original list is: “ +str(my_list))

random_index = random.randint(0, len(my_list)-1)

random_num = my_list[random_index]

print (“Random selected integer is :” +str(Random_num))

Again, save and execute the “SelectRandomNum.py” file to view the randomly selected number on the console screen:

Conclusion:

In this article, we have discussed different methods to access randomly selected elements from a Python list. We’ve retrieved the list using random operations, i.e., random.choice(), randrange(), and randint(). You can also access random elements using random.random() and random.sample(). We hope you found this article helpful. Please check out other informative articles at Linux Hint.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content