Python Programming

random.sample() in Python

random.sample() in Python

Introduction

The built-in function sample() of the random module in Python returns a specific length list of objects selected from the sequence, such as a list, tuple, string, or set. used for replacement-free random sampling.

The sample() function in the random module of Python allows for random sampling, which selects many elements at random from a list without repeating any of the elements. It gives back a collection of distinct things drawn at random from the list, sequence, or set. Random sampling without replacement is what we call it.

For instance, if you have a list of 100 names and you want to pick ten names at random from it without using any of the same names twice, you must use random.sample.

Operation

Description

random.sample(seq, n)

Generate n unique samples (multiple items) from a sequence without repetition. Here, A sequence can be a list, a set, a string, or a tuple. Sample without replacement.

random.choices(seq, n)

Generate n samples from a sequence with the possibility of repetition. Sample with replacement

random.sample(range(100), 5)

The sampled list of randomly generated integers is returned.

random.sample(d1.items(), 2)

Returns two key-value pairs from the Python dictionary.

How to use random.sample()

A fresh list with the items chosen at random is what is returned.

Syntax

random.sample(population, k)

Arguments

Both of the two arguments are needed for the sample() function.

  • Population: The sequence from which you want to choose a k-length number can be any sequence, including a list, set, or string.
  • The number you want to choose at random from the sequence, k. The value of k must be less than the length of the given list.
  • If any of the necessary arguments are omitted, a TypeError I s raised.

Example of using random sample() to select multiple items from a list without repetition

import random

aList = [20, 40, 80, 100, 120]
sampled_list = random.sample(aList, 3)
print(sampled_list)

Output:

[20, 80, 120]

The sample() function, as seen in the output, does not repeat the list items. It's also known as a random sample with no replacement. As a result, use it to generate random samples with no repetitions.

write your code here: Coding Playground

Points to remember

It has no effect on the provided sequence or list. It generates a new sampled list with elements from the supplied sequence or list.

It is not necessary for the supplied list or sequence to be hashable or unique.

Important: If your list contains repeated or duplicate elements, sample() will choose them all because each occurrence is a possible selection in the sample. If the number of unique members is fewer than a certain threshold, it selects the repeated items from the supplied list.

Example:

import random

exampleList = [20, 40, 20, 20, 40, 60, 70]
# choosing 4 random items from a list
sampled_list2 = random.sample(exampleList, 4)
print(sampled_list2)

Output:

[60, 20, 20, 40]

Random sample with replacement to including repetitions

import random

names = ["Roger", "Nadal", "Novac", "Andre", "Sarena", "Mariya", "Martina"]
# choose three random sample with replacement to including repetition
sample_list3 = random.choices(names, k=3)
print(sample_list3)

Output:

['Martina', 'Nadal', 'Martina']

write your code here: Coding Playground

Generate the sampled list of random integers

import random

# create list of 5 random numbers
num_list = random.sample(range(100), 5)
print(num_list)

Output:

[79, 49, 6, 4, 57]

On top of that, you may shuffle the list of random integers using random.shuffle().

import random

# create list of 5 numbers
num_list = random.sample(range(100), 10)
random.shuffle(num_list)
print(num_list)

Output:

[90, 36, 63, 37, 23, 11, 30, 68, 99, 5]

We used range() in conjunction with random.sample to generate a list of unique random integers since it is quick, memory-efficient, and increases performance when sampling from a big population.

write your code here: Coding Playground

A random sample from the Python set

A Python set is an unsorted collection of distinct things. We can select random samples from a set in the same way that we can from a list. Let's look at how to choose three random things from a set.

import random

aSet = ["Jhon", "kelly", "Scoot", "Emma", "Eric"]
# random 3 samples from set
sampled_set = random.sample(aSet, 3)
print(sampled_set)

Output:

['Emma', 'kelly', 'Eric']

write your code here: Coding Playground

Random sample from Python dictionary

The sample() function expects the population to be a sequence or set, which the dictionary is not. If you pass dict directly, you will receive a TypeError: Population must be a series or set.

So it's preferable if you use dict.items() to retrieve a list of all the dictionary elements and provide it to sample() together with the sampling size .

Let's look at an example of selecting two key-value pairs from a dictionary.

import random

marks_dict = {
    "Kelly": 55,
    "jhon": 70,
    "Donald": 60,
    "Lennin": 50
}
sampled_dict = random.sample(sorted(marks_dict.items()), 2)
print(sampled_dict)



# Access key-value from sample
# First key:value
print(sampled_dict[0][0], sampled_dict[0][1])



# Second key:value
print(sampled_dict[1][0], sampled_dict[1][1])

Output:

[('Donald', 60), ('jhon', 70)]


jhon 70


Kelly 55

write your code here: Coding Playground

Random seed to achieve the same sample list every time

Seed the random generator so that it always produces the same sampled list of items from the supplied list.

Every time, use the same seed value to receive the same sampled list.

import random

# Randomly select same sample list every time
alist = [20.5, 40.5, 30.5, 50.5, 70.5]

for i in range(3):
    # use 4 as a seed value
    random.seed(4)
    # get sample list of three item
    sample_list = random.sample(alist, 3)
    print(sample_list)

Output:

[40.5, 30.5, 20.5]
[40.5, 30.5, 20.5]
[40.5, 30.5, 20.5]

write your code here: Coding Playground

random.sample() function Error and exception

A sample function can cause the two faults listed below.

  • If the sample size is greater than the population or sequence (i.e., list or set) size, a ValueError occurs.
  • If one of the two arguments is missing, a type error occurs

Conclusion

We hope you found our post interesting and learned random.sample(), and hope you can use the same in your own code.