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.
Methodology: The Fast Loaded Dice Roller
The FLDR algorithm resolves this trade-off through a two-step "Dyadic Proposal" process:
- 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.
- 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.
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