![random.sample() in Python](/blog/content/images/2023/03/random.sample.png)
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.
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
Output:
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.
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:
Output:
Random sample with replacement to including repetitions
Output:
Generate the sampled list of random integers
Output:
On top of that, you may shuffle the list of random integers using random.shuffle().
Output:
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.
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.
Output:
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.
Output:
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.
Output:
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.