Skip to main content
QUICK REVIEW

[Paper Review] Random Sampling: Practice Makes Imperfect

Philip B. Stark, Kellie Ottoboni|arXiv (Cornell University)|Oct 25, 2018
Chaos-based Image/Signal EncryptionComputer Science13 references5 citations
TL;DR

This paper demonstrates that widely used pseudo-random number generators (PRNGs) in statistical software—such as the Mersenne Twister in R, Python, and MATLAB—have insufficient state spaces to generate all possible random samples or permutations for moderate-to-large datasets, leading to biased sampling. The authors advocate replacing standard PRNGs with cryptographically secure PRNGs by default and adopting bit-masking algorithms instead of flawed multiply-and-floor methods for integer generation, significantly improving statistical accuracy and reproducibility in inference tasks.

ABSTRACT

The pseudo-random number generators (PRNGs), sampling algorithms, and algorithms for generating random integers in some common statistical packages and programming languages are unnecessarily inaccurate, by an amount that may matter for statistical inference. Most use PRNGs with state spaces that are too small for contemporary sampling problems and methods such as the bootstrap and permutation tests. The random sampling algorithms in many packages rely on the false assumption that PRNGs produce IID $U[0, 1)$ outputs. The discreteness of PRNG outputs and the limited state space of common PRNGs cause those algorithms to perform poorly in practice. Statistics packages and scientific programming languages should use cryptographically secure PRNGs by default (not for their security properties, but for their statistical ones), and offer weaker PRNGs only as an option. Software should not use methods that assume PRNG outputs are IID $U[0,1)$ random variables, such as generating a random sample by permuting the population and taking the first $k$ items or generating random integers by multiplying a pseudo-random binary fraction or float by a constant and rounding the result. More accurate methods are available.

Motivation & Objective

  • To investigate whether commonly used pseudo-random number generators (PRNGs) in statistical software are adequate for modern sampling problems.
  • To expose the statistical bias introduced by flawed sampling algorithms that assume PRNG outputs are perfectly IID U[0,1) variables.
  • To demonstrate that PRNGs like the Mersenne Twister cannot generate all permutations or samples of even moderately sized datasets due to limited state space.
  • To identify and critique the multiply-and-floor method for generating random integers, which produces non-uniform distributions even with uniform PRNG inputs.
  • To recommend best practices for statistical software, including migration to cryptographically secure PRNGs and adoption of bit-masking algorithms for integer generation.

Proposed method

  • Analyzing the state space size of common PRNGs (e.g., Mersenne Twister, 32-bit LCGs) to determine their ability to generate all permutations or samples of size k from a population of size n.
  • Evaluating the statistical behavior of sampling algorithms that rely on permuting data or multiplying PRNG outputs by a constant and rounding (multiply-and-floor method).
  • Comparing the performance of the multiply-and-floor method against the bit-masking method for generating uniformly distributed integers on {1,…,m}, showing the latter avoids bias.
  • Measuring the L₁ distance between the theoretical uniform distribution over samples and the actual distribution induced by a given PRNG and sampling algorithm, finding it can approach 2.
  • Proposing the use of cryptographically secure PRNGs (CS-PRNGs) by default in statistical software, not for security but for superior statistical properties.
  • Implementing and releasing a prototype CS-PRNG in Python (cryptorandom) to demonstrate feasibility and encourage adoption in R and Python ecosystems.

Experimental results

Research questions

  • RQ1Can common PRNGs like the Mersenne Twister generate all possible samples of size k from a population of size n for moderate n and k?
  • RQ2To what extent does the multiply-and-floor method for generating random integers introduce bias, even when PRNG outputs are uniformly distributed?
  • RQ3How large is the statistical discrepancy (measured by L₁ distance) between the theoretical uniform distribution over samples and the actual distribution produced by standard software?
  • RQ4Are there practical, detectable biases in common statistical procedures (e.g., bootstrap, permutation tests) due to PRNG limitations in real-world replication counts (e.g., 10⁵)?
  • RQ5Can the use of cryptographically secure PRNGs and bit-masking algorithms eliminate or significantly reduce sampling bias in statistical software?

Key findings

  • PRNGs with finite state spaces, including the Mersenne Twister, cannot generate all permutations of 2084 or more items, limiting their use in large-scale resampling.
  • The multiply-and-floor method for generating random integers—used by R’s sample() and Python’s random.choice()—produces non-uniform distributions, even when PRNG outputs are uniformly distributed on w-bit integers.
  • The L₁ distance between the theoretical uniform distribution over samples and the actual distribution induced by flawed PRNGs and algorithms can be as high as nearly 2, indicating substantial statistical bias.
  • Even for datasets with a few hundred observations, common PRNGs fail to generate all possible subsets of size k, undermining the validity of resampling methods like the bootstrap and permutation tests.
  • The bit-masking method for generating random integers—where bits are masked and rejected if out of range—produces uniformly distributed integers and should replace the multiply-and-floor method.
  • Cryptographically secure PRNGs, while slower, offer superior statistical properties and should be used by default in statistical software to prevent bias, with weaker PRNGs available only as an opt-in option.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.