Python random sample() to choose multiple items from any sequence

Learn how to use Python’s random sample() function to select multiple items from any sequence such as a list, tuple or string. Discover various ways to generate random integers, floating point values, and samples from Python dictionary.

I. How to use it?

The python random module provides the sample() function which can be used to choose multiple items from any sequence (such as a list, tuple, or string). The sample() function takes two arguments: the population from which to select items and the number of items to select. Here is an example of how to use the sample() function to select 3 items from a list:

import random

my_list = [1, 2, 3, 4, 5, 6]
random_items = random.sample(my_list, 3)
print(random_items)

II. Generate the sampled list of random integers

It is also possible to generate a list of random integers using the sample() function. The following example shows how to generate a list of 5 random integers between 1 and 10:

import random

random_ints = random.sample(range(1, 11), 5)
print(random_ints)

III. Using Random Module in Python

In addition to the sample() function, the random module in Python also provides a variety of other functions for generating random numbers and selecting random items from sequences. Here are a few examples:

IV. Random sample from Python dictionary

It is also possible to select random items from a Python dictionary. Here is an example that selects 3 random items from a dictionary:

import random

my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
random_items = random.sample(my_dict.items(), 3)
print(random_items)

V. Generating random floating point values

The random module also provides the uniform() function for generating random floating point values between a given range. Here is an example that generates a random float between 0 and 1:

import random

random_float = random.uniform(0, 1)
print(random_float)

VI. Random sample with replacement to include repetitions

The sample() function by default samples without replacement, but it is possible to sample with replacement by setting the replace argument to True. Here is an example that samples 3 items from a list with replacements:

import random

my_list = [1, 2, 3, 4, 5, 6]
random_items = random.sample(my_list, 3, replace=True)
print(random_items)

VII. Random seed to get the same sample list every time

To generate the same random numbers every time, you can use the random.seed() function. This function takes an integer as an argument, and initializes the random number generator with the given value. Here is an example that generates a list of 5 random integers between 1 and 10, and then generates the same list again using the same seed value:

import random

random.seed(123)
random_ints1 = random.sample(range(1, 11), 5)

random.seed(123)
random_ints2 = random.sample(range(1, 11), 5)

print(random_ints1)
print(random_ints2)

VIII. Computing normally distributed numbers with random.gauss()

The random module provides the random.gauss() function for generating random numbers from a normal distribution (also known as a Gaussian distribution or a bell curve). The function takes two arguments, the mean and the standard deviation of the distribution. Here is an example that generates a list of 5 random numbers from a normal distribution with a mean of 0 and a standard deviation of 1:

import random

random_nums = [random.gauss(0, 1) for _ in range(5)]
print(random_nums)

IX. Picking random items in a list using random.choice()

The random module also provides the random.choice() function for picking a random item from a list or sequence. Here is an example that picks a random item from a list:

import random

my_list = [1, 2, 3, 4, 5, 6]
random_item = random.choice(my_list)
print(random_item)

X. Generating random integers using random.randint()

The random module also provides the random.randint() function for generating a random integer between two given values. Here is an example that generates a random integer between 1 and 10:

import random

random_int = random.randint(1, 10)
print(random_int)

XI. Randomly rotating items in a list using random.shuffle()

The random module also provides the random.shuffle() function for randomly shuffling the items in a list. Here is an example that shuffles the items in a list:

import random

my_list = [1, 2, 3, 4, 5, 6]
random.shuffle(my_list)
print(my_list)

XII. A random sample from the Python set

It is also possible to select random items from a Python set using the sample() function. Here is an example that selects 3 random items from a set:

import random

my_set = {1, 2, 3, 4, 5, 6}
random_items = random.sample(my_set, 3)
print(random_items)

Conclusion

In this article, we have covered the various ways in which we can use the random.sample() function in Python to select multiple items from any sequence. We also looked at other useful functions provided by the random module, such as random.gauss(), random.choice(), random.randint(), random.shuffle(), random.seed() for generating random numbers and selecting random items from sequences. With the help of these functions, we can generate random numbers and select random items from any sequence in an easy and efficient way.

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment