Fast Dice Roller Algorithms
- Fast Dice Roller is a family of algorithms that simulate exact n-sided dice rolls from coin flips by recycling unused entropy to nearly eliminate information loss.
- The methods maintain an entropy pool (state register) where residual bits are stored, allowing variable die sizes and expected constant-time performance.
- Variants, including bounded-time and Knuth–Yao styled implementations, achieve near-optimal efficiency while offering formal correctness proofs via distributional invariants.
Fast Dice Roller denotes a family of exact algorithms for simulating a fair roll of an -sided die from coin flips. In the formulation of Ömer and Pacher, the central device is an entropy pool held in a register: entropy that is generated as a by-product during each die roll and that is usually discarded is instead stored and completely reused during the next rolls, yielding an almost negligible loss of entropy per roll; the same mechanism also permits the number of die sides to change from one round to the next (Ömer et al., 2014). The label has subsequently also been used for distinct but related constructions, including a bounded-time method using one -coin plus fair flips and a Knuth–Yao-style recycler for the uniform case (Viglietta, 2015).
1. Recycler-state formulation
In the entropy-pool formulation, the algorithm maintains a “die state” . Here is the size of the pool, interpreted as the number of equally likely states, and is the current pool content, an integer in . The state is commonly initialized with for some word-size , such as $32$ or 0, and 1 is interpreted as the binary value of the bits drawn so far (Ömer et al., 2014).
To draw a fair 2-sided roll, the algorithm first ensures that the pool is large enough. If 3, it refills by appending fair random bits; each appended bit 4 updates the state by
5
Once 6, it computes
7
If 8, the state falls in a valid 9 region. The output is
0
which is uniform in 1, and the leftover pool is updated to the complementary roll,
2
If instead 3, the sample would bias the result if used. The unused part is therefore recycled by
4
and the procedure continues, refilling again only if necessary (Ömer et al., 2014).
A common misconception is that exact die simulation by rejection must discard all information in an out-of-range sample. In this recycler formulation, the tail 5 is not discarded; it is converted into a smaller residual pool and carried forward. The stated key point is that no entropy is ever destroyed except for the tiny amount learned by the overflow test 6 (Ömer et al., 2014).
2. Entropy accounting and efficiency
The entropy analysis is explicit. Let
7
denote the binary entropy, and define
8
the probability of an immediate accept on any iteration. The average number of trials to succeed is 9, and each test of the predicate 0 leaks exactly 1 bits of entropy because it reveals one biased-coin outcome (Ömer et al., 2014).
The expected entropy waste per roll is therefore
2
Since a successful roll extracts exactly 3 bits of uniform output, the total cost is
4
As 5 grows, 6, hence 7 and 8; the efficiency correspondingly tends to 9. The paper further shows that 0 is strictly decreasing in 1, and that a large entropy pool minimizes the loss. In its own terms, the entropy loss is monotone decreasing with increasing entropy pool size, or register length (Ömer et al., 2014).
This analysis places the method near the information-theoretic lower bound for exact uniform sampling from fair bits. The important distinction is that the inefficiency is not concentrated in discarded whole words or discarded whole trials; it is localized to the information revealed by the accept-versus-overflow test.
3. Variable arity, cost model, and implementation practice
A distinctive property of the recycler formulation is that 2 may change on every call. Each invocation simply recomputes 3 and 4 from the current 5; the residual quantities 6 and 7 split the old pool into the new output and leftover entropy, so the pool need not be flushed unless that is explicitly desired (Ömer et al., 2014).
The time complexity is stated as expected 8 iterations per roll when 9 is close to 0, which occurs when 1. Each iteration performs one integer division and one comparison, together with occasional pool-refill loops whose amortized cost is 2 per bit output. The space usage is minimal: two machine-word registers 3, plus a small buffer if entropy is read in byte chunks (Ömer et al., 2014).
Several implementation details are emphasized. A 4-bit pool stores up to approximately 5 bits of simultaneous entropy and yields 6; larger 7 can be handled with a 8-bit pool via compiler intrinsics or double-width libraries. Entropy may be appended by bytes rather than single bits, preserving the same logic while reducing system-call overhead. If 9 is fixed, compilers often transform division and modulo into multiply-plus-shift; if 0 varies, hardware division remains the dominant constant factor, together with bit shifts or byte appends during refill. Overflow checks are required when shifting 1 or 2, and parallel use is handled either by one pool per thread or by protecting the state with a lock (Ömer et al., 2014).
In practical terms, the resulting interface provides fully unbiased output on 3, almost zero wasted entropy per roll as the pool grows, and a compact loop-and-divide core that persists across calls.
4. Other algorithms called “Fast Dice Roller”
The name is not unique to the entropy-pool algorithm. Viglietta’s “Fast Dice Roller” addresses the same sampling problem in a different resource model: it simulates a fair 4-sided die by one flip of a biased coin with probability 5 of heads, followed by exactly 6 flips of a fair coin (Viglietta, 2015). The construction first uses one 7-coin and 8 fair flips, where 9, to simulate a 0-coin, and then uses that virtual coin plus 1 additional fair flips to produce a uniform value in 2. Its cost is deterministic: 3 The paper explicitly notes that no rejection loops are needed and that every flip is used (Viglietta, 2015).
A different specialization appears in work on Knuth–Yao for the fair die. There the state is 4 with
5
initialized at 6. The algorithm repeatedly doubles until 7, accepts if 8, and otherwise recycles the overshoot by replacing 9 with 0. For this uniform-case recycler, the expected number of fair-coin flips satisfies
1
an improvement over the classic Knuth–Yao guarantee of 2. The in-place version uses only 3 bits of memory, while a precomputed prefix-tree implementation uses 4 memory (Huber et al., 2024).
This terminological overlap suggests that “Fast Dice Roller” functions less as the name of a single canonical algorithm than as a label for a class of exact die-rolling constructions that exploit recycling, structured decomposition, or both.
5. Relation to Knuth–Yao and to loaded dice
The broader theoretical backdrop is Knuth and Yao’s method for sampling a finite distribution from fair-coin flips. For the fair die, the 2024 specialization emphasizes that the algorithm can be run with memory linear in the input for a precomputed binary trie, or with only logarithmic memory in the recycler form; analysis yields a bound on the average number of coin flips that is slightly better than the original Knuth–Yao bound (Huber et al., 2024).
The non-uniform analogue is the Fast Loaded Dice Roller (FLDR). That algorithm addresses a target distribution 5, where the 6 are positive integers summing to 7, and uses only independent fair bits. It constructs a proposal distribution
8
with 9, and samples from the corresponding implicit Knuth–Yao discrete distribution generating tree, rejecting only the overflow leaf $32$0. The expected number of random bits used satisfies
$32$1
so the sampler consumes at most $32$2 bits more entropy per sample than the information-theoretically optimal rate. Its preprocessing runs in $32$3 time, the representation is near-linear in the input size, and the sampling time is expected $32$4 (Saad et al., 2020).
The relation between FDR and FLDR is conceptually direct: the latter is presented as a generalization from uniform sampling to arbitrary discrete distributions, implemented through a bitwise tree method rather than through the specific $32$5 register formulation.
6. Correctness proofs and distributional invariants
A recent line of work studies Fast Dice Roller algorithms as probabilistic programs and proves their correctness by distributional loop invariants. In that framework, the program state assigns integer values to $32$6, where $32$7 is the current power-of-two range and $32$8 is the candidate. The verified loop repeatedly doubles $32$9, appends one fair bit to 00, and, whenever 01, recycles by subtracting 02 from both 03 and 04 (Batz et al., 8 Sep 2025).
The key invariant is formulated over reachable subdistributions 05. For some fixed 06,
07
and for all 08,
09
In words, 10 remains fixed, 11 stays in the interval 12, 13 remains in range, and conditioned on the current 14, the candidate is uniformly distributed on 15. The precondition is a Dirac distribution at 16, and the postcondition is that the final value satisfies 17 (Batz et al., 8 Sep 2025).
The same verification framework includes proof rules for total and partial correctness and is also applied to FLDR. The paper’s stated significance is methodological: distributional invariants permit proof by preservation of uniformity over an infinite family of loops parameterized by 18, which cannot be handled by finite-state Markov-chain model checking (Batz et al., 8 Sep 2025).
Taken together, these developments place Fast Dice Roller methods at the intersection of exact sampling, entropy-efficient random-bit generation, and formal verification. The unifying theme across the variants is exactness in the random-bit model, but the concrete algorithms differ in whether they prioritize entropy recycling across calls, bounded worst-case flip counts, low-memory Knuth–Yao execution, or extension to arbitrary discrete distributions.