Pick two or more random elements from an array in Python

There are several real-time situations wherein you might want to select random elements from a list of items. For instance, suppose when we want to randomly make a group of 4 students out of 30 students in a classroom. Thus, in this tutorial, we will learn how to pick two or more random elements from an array in Python.

Picking random elements from an array: Method 1

An array in Python stores items of the same data type inside a single variable. In the first method, we will make use of the random. sample()  from the random module of Python. This function picks out N random elements from any iterable of Python such as list, array, set, or dictionary. Also, note that random.sample() function return only unique random elements. The syntax of this method is given below:

Syntax:
random.choice(iterable,n)

In the following mentioned code out of the 8 names of students in a classroom, if we want to select 3 random students then the value of n, in this case, is 3. The iterable in our case is an array. It returns an array of random items. Finally, we iterate over each random element using a for loop and display them.

import random
array=["tina","laura","james","andrew","lisa","edward","stanley","georgia"]
new_items=random.sample(array,3)
print("Random items selected are:")
for item in new_items:
    print(item)

Output:

Random items selected are:
lisa
tina
georgia

Picking random elements from an array: Method 2

In the second method, we make use of the random.choices() function that is similar to the random.choice()  function. However, this function will return duplicate random elements as well. It samples elements from the array with replacement. The syntax of the function is mentioned below.

Syntax:
random.choice(iterable,k)

In the above syntax, iterable can be array, list, set, or tuple. k is an optional argument mentioning the number of items to be selected from the array. In the following mentioned code, the iterable is an array and the value of k is 3. It returns random elements from the array consisting of duplicate elements.

import random
array=["tina","laura","james","andrew","lisa","edward","stanley","georgia"]
new_items=random.choices(array,k=3)
print("Random items selected are:")
for item in new_items:
    print(item)

Output:

Random items selected are:
james
james
tina

Picking random elements from an array: Method 3

In the last method, we use random.shuffle() method of the random module in Python. This method will shuffle the array in a random sequence every time the function is called. It takes the array whose elements are to be shuffled as an argument. After shuffling the elements, we will then iterate over each element of the new array using a for loop. The loop will display the first three elements of the new array.

import random
array=["tina","laura","james","andrew","lisa","edward","stanley","georgia"]
random.shuffle(array)
print("Random items selected are:")
for i in range(3):
    print(array[i])

Output:

Random items selected are:
tina
georgia
andrew

This is the end of the tutorial. Thus, in this tutorial, we have learned how to pick two or more random elements from an array in Python. Learn how to Pick a random element from an array in C++ by clicking the following mentioned link: Pick a random element from an array in C++

Leave a Reply

Your email address will not be published. Required fields are marked *