Python provides a robust ecosystem for generating random numbers and selecting random samples from datasets. Whether you are building a simple lottery script, conducting an A/B test for a web application, or training a deep learning model, understanding the nuances of random sampling is critical. The efficiency and statistical validity of your results depend heavily on choosing the right tool—be it the built-in random module, the high-performance NumPy library, or the data-centric Pandas framework.

Selecting a random sample involves picking a subset of items from a larger population such that every item (or specific groups of items) has a defined probability of being chosen. In Python, this is categorized into two main types: sampling without replacement (where items are unique) and sampling with replacement (where items can appear multiple times).

Quick Answer for Common Sampling Tasks

For those looking for immediate implementation, here are the most frequent commands:

  • Pick $k$ unique items (Without Replacement): Use random.sample(population, k).
  • Pick $k$ items with potential repeats (With Replacement): Use random.choices(population, k=k).
  • Fastest sampling for large numerical arrays: Use numpy.random.default_rng().choice(array, size=k, replace=False).
  • Sampling rows from a table: Use dataframe.sample(n=k).

The Foundations of Random Sampling with Python Built-in Module

The standard random module is part of the Python Standard Library and is the most accessible tool for general-purpose programming. It uses the Mersenne Twister algorithm as its core generator, producing 53-bit precision floats with a period of $2^{19937}-1$. While extremely fast and thread-safe, it is deterministic and unsuitable for cryptographic purposes.

Selecting Unique Elements Using random.sample

The random.sample() function is the go-to method for sampling without replacement. It ensures that no single element from the population is selected more than once in the output list.