The Fast Loaded Dice Roller: A Near-Optimal Exact Sampler for Discrete Probability Distributions

Feras A. Saad, Cameron E. Freer, Martin C. Rinard, Vikash K. Mansinghka

Abstract

Sampling from discrete probability distributions is a fundamental operation in randomized algorithms and simulations. While the Knuth-Yao algorithm provides a theoretically perfect bit-efficient sampler, its exponential space requirements often make it impractical for large-scale applications. We introduce the Fast Loaded Dice Roller (FLDR), a new algorithm that achieves near-optimal bit efficiency—guaranteed to be within 6 bits of the Shannon entropy—while requiring only linear space. By combining a dyadic proposal distribution with an efficient accept-reject step and fast integer arithmetic, FLDR outperforms existing methods like the Alias method in both speed and memory efficiency.

Introduction: The Efficiency Trade-off in Sampling

In modern computing, "rolling a loaded die"—or sampling from a discrete distribution with custom weights—is ubiquitous. An ideal sampler must satisfy three criteria: it should be exact (avoiding floating-point errors), bit-efficient (minimizing the consumption of raw random bits), and space-efficient (minimizing memory footprint).

Historically, algorithms have struggled to balance these needs. The classic Knuth-Yao algorithm (1976) is perfectly bit-efficient but relies on a Discrete Distribution Generating (DDG) tree that can grow exponentially relative to the input size. Conversely, methods like the Alias method are fast but often rely on floating-point approximations or waste significant random bits.

Comparison of entropy-optimal and suboptimal Discrete Distribution Generating (DDG) trees, illustrating the structural complexity trade-offs.

Methodology: The Fast Loaded Dice Roller

The FLDR algorithm resolves this trade-off through a two-step "Dyadic Proposal" process:

  1. Dyadic Proposal Construction: Instead of targeting the desired distribution $p$ directly, FLDR constructs a proposal distribution $q$ composed of dyadic rationals (probabilities that are powers of 2). This ensures that the resulting decision tree is small and linear in size.
  2. Accept-Reject Correction: The algorithm samples from $q$ and uses a clever accept-reject step to correct the output to match the target distribution $p$.

Crucially, the implementation avoids explicit tree structures in memory. Instead, it uses fast integer arithmetic (bit-shifting and additions) to simulate tree traversal, making it highly efficient on modern hardware.

Key Insights and Theoretical Guarantees

The authors provide several formal proofs that distinguish FLDR from its predecessors:

  • Linear Space Complexity: The FLDR tree is guaranteed to have at most $2(n+1)\lceil \log m \rceil$ nodes, where $n$ is the number of outcomes and $m$ is the normalizer.
  • Near-Optimal Bit Consumption: FLDR uses at most $H(p) + 6$ bits in expectation. Maintaining this proximity to the Shannon entropy $H(p)$ while ensuring linear space is a significant theoretical advancement.
  • Direct Simulation: By using compact arrays to store leaf counts at each level, the algorithm achieves extremely fast traversal without the overhead of pointer-based graph structures.
Structural differences between optimal and suboptimal DDG trees, highlighting the leaf distribution used in sampling.

Experimental Results

The algorithm was evaluated against synthetic distributions with varying entropy and scale. Key findings include:

  • Performance: FLDR is 2x to 10x faster in both preprocessing and sampling than common baselines like the Alias method.
  • Memory Efficiency: It requires up to 10,000x less space than the entropy-optimal Knuth-Yao sampler for large distributions.
  • Bit Efficiency: Unlike the Alias method, which has a constant bit cost, FLDR’s bit consumption scales with the complexity of the distribution, closely tracking the theoretical lower bound.

Conclusion and Future Directions

FLDR provides a robust solution for exact sampling, particularly for cryptographic applications and high-precision simulations where floating-point errors are unacceptable. While it currently assumes rational probabilities (integer weights), future work may extend these guarantees to broader classes of inputs. Furthermore, the algorithm's reliance on simple integer arithmetic makes it an ideal candidate for specialized hardware implementations on FPGAs.

Summary updated

Abstract

This paper introduces a new algorithm for the fundamental problem of generating a random integer from a discrete probability distribution using a source of independent and unbiased random coin flips. We prove that this algorithm, which we call the Fast Loaded Dice Roller (FLDR), is highly efficient in both space and time: (i) the size of the sampler is guaranteed to be linear in the number of bits needed to encode the input distribution; and (ii) the expected number of bits of entropy it consumes per sample is at most 6 bits more than the information-theoretically optimal rate. We present fast implementations of the linear-time preprocessing and near-optimal sampling algorithms using unsigned integer arithmetic. Empirical evaluations on a broad set of probability distributions establish that FLDR is 2x-10x faster in both preprocessing and sampling than multiple baseline algorithms, including the widely-used alias and interval samplers. It also uses up to 10000x less space than the information-theoretically optimal sampler, at the expense of less than 1.5x runtime overhead.
BibTeX
@inproceedings{saad2020fast,
  author = {Feras A. Saad and Cameron E. Freer and Martin C. Rinard and Vikash K. Mansinghka},
  title = {{The Fast Loaded Dice Roller: A Near-Optimal Exact Sampler for Discrete Probability Distributions}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2020},
  url = {https://openreview.net/forum?id=--ID7IGVR_},
}
Figure