Random Variate Generation with Formal Guarantees

Feras A. Saad, Wonyeol Lee

This paper, presented at PLDI 2025, addresses a fundamental discrepancy in computer science: the gap between the mathematical theory of probability and its practical implementation in software. While textbook probability assumes infinite precision, our software libraries are built on a mathematical lie that often leads to subtle, hard-to-debug errors.

The Precision Gap

In theoretical models, we assume the ability to sample a real number $u$ uniformly from the interval $[0, 1]$ and transform it into any desired distribution. However, physical hardware operates on finite-precision floating-point numbers and discrete bit streams. Standard libraries (like GSL, NumPy, or PyTorch) bridge this gap with heuristics that often break down under scrutiny.

Comparison of infinite-precision (Real-RAM) vs. finite-precision random variate generation models.

As shown above, the transition from infinite to finite precision changes the fundamental nature of the entropy source and the resulting output. The authors surveyed nearly 90 user-reported issues in popular Python libraries, finding that almost all were related to these fundamental numerical errors, such as the inability to reach the extreme "tails" of a distribution—a critical failure for fields like cryptography and differential privacy.

The Consistency Crisis

One of the most pervasive issues identified is inconsistency. In standard numerical implementations, a library’s Cumulative Distribution Function (CDF) and its random generator might represent slightly different distributions due to rounding errors. This "numerical drift" means that the software's behavior does not strictly match its mathematical specification.

The proposed approach (c) restores consistency between the probability measure and its various representations compared to the standard approach (b).

A Specification-First Solution

The authors propose a "specification-first" approach. Instead of writing an ad-hoc algorithm to generate a random variable, the developer provides a numerical program that implements the Cumulative Distribution Function (CDF). The system then automatically synthesizes a generator that is guaranteed to be "exact" relative to that specification.

Key technical components include:

  • Binary-Coded Distributions: A universal mathematical representation for any binary number format (floating-point, fixed-point, or posits).
  • Lazy DDG Tree Refinement: Based on the Knuth and Yao "Discrete Distribution Generating" (DDG) tree, this algorithm only explores the paths needed for the current output, achieving optimal space-time complexity.
  • Dual Distribution Functions (DDF): To handle the high precision required near zero in floating-point formats, the system combines the CDF and the Survival Function into a single coherent specification.

Key Results and Impact

The paper evaluates the method using a benchmark suite of 18 continuous and 6 discrete distributions. The results demonstrate that formal exactness does not have to come at the cost of performance:

  • Entropy Efficiency: The synthesized generators are 2.6x to 142x more entropy-efficient than the industry-standard GNU Scientific Library (GSL), meaning they consume significantly fewer random bits.
  • Accuracy: Unlike standard libraries, these generators have zero approximation error relative to the specified CDF.
  • Tail Coverage: The method can cover intervals up to $10^{35}$ times wider than GSL, ensuring that rare events in the tails of distributions are correctly represented.

While bit-by-bit processing can be slower in raw "variates per second" than specialized hardware instructions, the process is fully automated. If a developer can write the CDF, the system can build a generator with formal guarantees, providing a robust foundation for the next generation of probabilistic programming languages.

Reference: Saad, F. A., & Lee, W. (2025). Random Variate Generation with Formal Guarantees. Proc. ACM Program. Lang., Vol. 9, No. PLDI, Article 152.

Summary updated

BibTeX
@article{saad2025random,
  author = {Feras A. Saad and Wonyeol Lee},
  title = {{Random Variate Generation with Formal Guarantees}},
  journal = {Proc. ACM Program. Lang.},
  year = {2025},
  doi = {10.1145/3729251},
}
Figure