Python random.sample() function


The random.sample() function in Python is part of the built-in random module and is used to generate a random sample from a specified population. Unlike random.choice(), which selects a single random element, random.sample() allows you to select multiple unique elements from a sequence without replacement. This means that the same element cannot be selected more than once in a single call.

Syntax

import random sample = random.sample(population, k)
  • Parameters:
    • population: A sequence (like a list, tuple, or string) from which to sample.
    • k: The number of unique elements to select from the population. Must be less than or equal to the size of the population.

Return Value

  • Returns a list of k unique elements chosen from the population.

Example Usage

  1. Basic Example:

    import random population = [1, 2, 3, 4, 5] sample = random.sample(population, 3) # Select 3 unique elements print(sample) # Example output: [2, 5, 1]
  2. Sampling from a List of Strings:

    fruits = ['apple', 'banana', 'cherry', 'date', 'fig'] sample = random.sample(fruits, 2) # Select 2 unique fruits print(sample) # Example output: ['banana', 'fig']
  3. Sampling with a Larger Population:

    population = range(1, 101) # Population of numbers from 1 to 100 sample = random.sample(population, 5) # Select 5 unique numbers print(sample) # Example output: [24, 78, 3, 56, 19]
  4. Attempting to Sample More Than Available: If you try to sample more elements than are available in the population, a ValueError will be raised:

    try: sample = random.sample(fruits, 10) # This will raise an error except ValueError as e: print(e) # Output: Sample larger than population or is negative
  5. Seeding the Random Number Generator: You can use random.seed() to ensure reproducibility:

    random.seed(42) # Seed the random number generator sample = random.sample(population, 3) print(sample) # Output will be the same each time the seed is set

Summary

  • random.sample(population, k) is a versatile function for selecting multiple unique elements from a given sequence.
  • It is useful in various applications, such as random sampling, simulations, and games.
  • The function raises a ValueError if k is larger than the size of the population, so it's important to ensure that you specify a valid sample size.