What is random.sample() Method in Python

To select a single random element from the list in Python, use the random.choice() function.

Python random.sample

The random.sample() is a built-in Python function that returns a specific length of list chosen from the sequence like list, tuple, string, or set. To pick a random sample from the sequence in Python, use the random.sample() method.

The random.sample() function is used for random sampling and randomly picks more than one item from the list without repeating elements.

The random.sample() returns the list of unique items chosen randomly from the list, sequence, or set, and we call it random sampling without replacement.

If your list contains repeated or duplicate items, then random.sample() can choose repeated items because each occurrence is the possible selection in the sample.

The random.sample() can pick the repeated elements from the specified list if the unique members are less than the sampling size.

Syntax

random.sample(sequence, k)

Parameters

The sequence can be any sequence, such as a list, set, dictionary, tuple, or string from which we can select a k-length number. The sequence is a required parameter.

The k is the number of random elements you want to select from the sequence. The k must be less than the size of the specified list.

Return Value

The random.sample() function returns the new list containing randomly selected elements.

Example

import random

data = [21, 19, 18, 46, 29]
print("Choosing 3 random elements from the list using random.sample() function")
sampled_output = random.sample(data, 3)
print(sampled_output)

Output

Choosing 3 random elements from a list using random.sample() function
[19, 46, 21]

In this example, first, we have imported a random module.

Then defined, a list contains unique values.

Then, we used the random.sample() function and pass the list and 3 as an argument because we want to choose three random elements.

At last, we printed those three elements in the console.

As you can see, the random.sample() method doesn’t repeat the elements in the output list. This is also called a random sample without replacement.

Random.sample() raises ValueError

If we pass a Sample larger than the population or is negative, the sample() function will throw an error.

For example, we defined a list of five elements in the above code. Now, if we pass the k argument = 6, it will larger the sequence elements. And that is why it will throw an error.

import random

data = [21, 19, 18, 46, 29]
print("Choosing 6 random elements from the list using random.sample() function")
sampled_output = random.sample(data, 6)
print(sampled_output)

Output

Choosing 6 random elements from the list using random.sample() function
Traceback (most recent call last):
  File "app.py", line 5, in <module>
    sampled_output = random.sample(data, 6)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/random.py", line 363, in sample
    raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative

You got the ValueError: Sample larger than population or is negative.

That means the k argument must be less than the size of the specified list.

A sample function can raise the following two errors.

  1. ValueError If the sample size is larger than the population (i.e., list or set) size.
  2. TypeError if any of the two arguments is missing.

The sample() function doesn’t modify the specified sequence or list. It returns a new sampled list containing elements from the specified sequence or list.

The specified list or population need not be hashable or unique.

Generating a sampled list of random integers

You can create a list of unique random integers using random.sample() method.

To create a list of random numbers without duplicate values, we have to use the combination of range() method and random.sample() function.

import random

print("Create a sampled list of random integers without duplicates")
sampled_op = random.sample(range(50), 5)
print(sampled_op)

Output

Create a sampled list of random integers without duplicates
[19, 29, 38, 28, 16]

In this example, we imported a random module and then used the range() function to create a range of integers from 0 to 50; we sampled the data using the sample() method.

So, we have generated five unique random integers using the range() and sample() methods.

This approach is beneficial when you have to generate unique random numbers from a specified range.

You can also use the random.shuffle() to shuffle the list of a sample of random integers.

import random

print("Create a sampled list of random integers without duplicates")
sampled_op = random.sample(range(50), 5)
print(sampled_op)
print('After shuffled output')
random.shuffle(sampled_op)
print(sampled_op)

Output

Create a sampled list of random integers without duplicates
[46, 2, 15, 1, 5]
After shuffled output
[46, 2, 1, 15, 5]

A random sample from Python tuple

To get the random samples from Python tuple, use the random.sample() method.

import random

SPIs = (5.4, 5.7, 7.3, 7.7, 8.3, 9.0, 8.8, 8.4)

print('Choosing 4 random elements from a tuple using random.sample()')

sampled_op = random.sample(SPIs, 4)
print(sampled_op)

Output

[5.7, 7.7, 8.3, 8.8]

In this example, we have defined a tuple and then choose four elements from the list using the sample() method.

A random sample from Python dictionary

Python random sample() function works with sequence, and the dictionary is not a sequence.

If you try to pass the dictionary directly, you will get TypeError: Population must be a sequence or set. For dicts, use list(d).

import random

apple = {
    'Eleven': 'Millie',
    'Mike': 'Finn',
    'Dustin': 'Gaten',
    'Will': 'Noah'
}

sampled_op = random.sample(apple, 2)
print(sampled_op)

Output

Traceback (most recent call last):
  File "app.py", line 8, in <module>
    sampled_op = random.sample(apple, 1)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/random.py", line 359, in sample
    raise TypeError("Population must be a sequence or set.  For dicts, use list(d).")
TypeError: Population must be a sequence or set.  For dicts, use list(d).

To avoid this, we can use the function dictionary.items().

Python dictionary items() is an inbuilt function that is used to get all the keys and associated values with those keys.

So, you can pass the dict.items() to the sample() function, and it will return the list of random key-value pairs.

import random

apple = {
    'Eleven': 'Millie',
    'Mike': 'Finn',
    'Dustin': 'Gaten',
    'Will': 'Noah'
}

sampled_op = random.sample(apple.items(), 2)
print(sampled_op)

Output

[('Eleven', 'Millie'), ('Mike', 'Finn')]

In this example, instead of passing a dictionary, we pass the dictionary.items(). And in return, we get the list of tuple items to consist of key and value.

A random sample from the Python set

To select random samples out of a set, use the Python random.sample() method.

import random

SPIs = {5.4, 5.7, 7.3, 7.7, 8.3, 9.0, 8.8, 8.4}

print('Choosing 4 random items from a set using random.sample()')

sampled_op = random.sample(SPIs, 4)
print(sampled_op)

Output

Choosing 4 random items from a set using random.sample()
[7.7, 7.3, 8.4, 8.3]

In this example, we have defined a Python set and then choose four elements from the list using the sample() method.

Python random sample seed

To get the same sampled list of elements every time from the specified list. We can do this by using random.seed() and random.sample() function together.

import random

SPIs = [5.4, 5.7, 7.3, 7.7, 8.3, 9.0, 8.8, 8.4]

random.seed(4)

sampled_op = random.sample(SPIs, 4)
print(sampled_op)

Output

➜  pyt python3 app.py
[7.7, 7.3, 5.4, 8.4]
➜  pyt python3 app.py
[7.7, 7.3, 5.4, 8.4]
➜  pyt python3 app.py
[7.7, 7.3, 5.4, 8.4]
➜  pyt python3 app.py
[7.7, 7.3, 5.4, 8.4]

From the output, you can see that every time we run the program, we get the same output.

Get a sample array from the sizeable multidimensional array

To get a sample array from a multidimensional array, we will use the numpy.random.choice() method to pick multiple random rows from a multidimensional array.

import numpy

array = numpy.array(
    [[21, 41, 16], [15, 10, 25], [16, 19, 18], [71, 14, 21], [81, 16, 24]])
print("Printing 2D Array")
print(array)

print("Choose multiple random row from 2D array")
randomRows = numpy.random.randint(5, size=2)
for i in randomRows:
    print(array[i, :])

Output

Printing 2D Array
[[21 41 16]
 [15 10 25]
 [16 19 18]
 [71 14 21]
 [81 16 24]]
Choose multiple random row from 2D array
[71 14 21]
[15 10 25]

In this example, first, we have defined a 2D array, and then we have used the numpy.random.randint() method to choose the random row from the 2D array and then print that random row using for loop.

Conclusion

To get the random sample of elements from the sequence like Python list, tuple, set, array, or multidimensional array, you can use the Python random sample() method.

See also

Python random choice()

Python random number module

Numpy random rand()

Numpy random randn()

Numpy random shuffle()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.