Papers
Topics
Authors
Recent
Search
2000 character limit reached

Fast Dice Roller Algorithms

Updated 10 July 2026
  • 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 nn-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 1n\tfrac1n-coin plus 3log2n+13\lfloor\log_2 n\rfloor+1 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” (m,t)(m,t). Here mm is the size of the pool, interpreted as the number of equally likely states, and tt is the current pool content, an integer in [0,m)[0,m). The state is commonly initialized with m=2wm=2^w for some word-size ww, such as $32$ or 1n\tfrac1n0, and 1n\tfrac1n1 is interpreted as the binary value of the bits drawn so far (Ömer et al., 2014).

To draw a fair 1n\tfrac1n2-sided roll, the algorithm first ensures that the pool is large enough. If 1n\tfrac1n3, it refills by appending fair random bits; each appended bit 1n\tfrac1n4 updates the state by

1n\tfrac1n5

Once 1n\tfrac1n6, it computes

1n\tfrac1n7

If 1n\tfrac1n8, the state falls in a valid 1n\tfrac1n9 region. The output is

3log2n+13\lfloor\log_2 n\rfloor+10

which is uniform in 3log2n+13\lfloor\log_2 n\rfloor+11, and the leftover pool is updated to the complementary roll,

3log2n+13\lfloor\log_2 n\rfloor+12

If instead 3log2n+13\lfloor\log_2 n\rfloor+13, the sample would bias the result if used. The unused part is therefore recycled by

3log2n+13\lfloor\log_2 n\rfloor+14

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 3log2n+13\lfloor\log_2 n\rfloor+15 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 3log2n+13\lfloor\log_2 n\rfloor+16 (Ömer et al., 2014).

2. Entropy accounting and efficiency

The entropy analysis is explicit. Let

3log2n+13\lfloor\log_2 n\rfloor+17

denote the binary entropy, and define

3log2n+13\lfloor\log_2 n\rfloor+18

the probability of an immediate accept on any iteration. The average number of trials to succeed is 3log2n+13\lfloor\log_2 n\rfloor+19, and each test of the predicate (m,t)(m,t)0 leaks exactly (m,t)(m,t)1 bits of entropy because it reveals one biased-coin outcome (Ömer et al., 2014).

The expected entropy waste per roll is therefore

(m,t)(m,t)2

Since a successful roll extracts exactly (m,t)(m,t)3 bits of uniform output, the total cost is

(m,t)(m,t)4

As (m,t)(m,t)5 grows, (m,t)(m,t)6, hence (m,t)(m,t)7 and (m,t)(m,t)8; the efficiency correspondingly tends to (m,t)(m,t)9. The paper further shows that mm0 is strictly decreasing in mm1, 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 mm2 may change on every call. Each invocation simply recomputes mm3 and mm4 from the current mm5; the residual quantities mm6 and mm7 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 mm8 iterations per roll when mm9 is close to tt0, which occurs when tt1. Each iteration performs one integer division and one comparison, together with occasional pool-refill loops whose amortized cost is tt2 per bit output. The space usage is minimal: two machine-word registers tt3, plus a small buffer if entropy is read in byte chunks (Ömer et al., 2014).

Several implementation details are emphasized. A tt4-bit pool stores up to approximately tt5 bits of simultaneous entropy and yields tt6; larger tt7 can be handled with a tt8-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 tt9 is fixed, compilers often transform division and modulo into multiply-plus-shift; if [0,m)[0,m)0 varies, hardware division remains the dominant constant factor, together with bit shifts or byte appends during refill. Overflow checks are required when shifting [0,m)[0,m)1 or [0,m)[0,m)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 [0,m)[0,m)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 [0,m)[0,m)4-sided die by one flip of a biased coin with probability [0,m)[0,m)5 of heads, followed by exactly [0,m)[0,m)6 flips of a fair coin (Viglietta, 2015). The construction first uses one [0,m)[0,m)7-coin and [0,m)[0,m)8 fair flips, where [0,m)[0,m)9, to simulate a m=2wm=2^w0-coin, and then uses that virtual coin plus m=2wm=2^w1 additional fair flips to produce a uniform value in m=2wm=2^w2. Its cost is deterministic: m=2wm=2^w3 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 m=2wm=2^w4 with

m=2wm=2^w5

initialized at m=2wm=2^w6. The algorithm repeatedly doubles until m=2wm=2^w7, accepts if m=2wm=2^w8, and otherwise recycles the overshoot by replacing m=2wm=2^w9 with ww0. For this uniform-case recycler, the expected number of fair-coin flips satisfies

ww1

an improvement over the classic Knuth–Yao guarantee of ww2. The in-place version uses only ww3 bits of memory, while a precomputed prefix-tree implementation uses ww4 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 ww5, where the ww6 are positive integers summing to ww7, and uses only independent fair bits. It constructs a proposal distribution

ww8

with ww9, 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 1n\tfrac1n00, and, whenever 1n\tfrac1n01, recycles by subtracting 1n\tfrac1n02 from both 1n\tfrac1n03 and 1n\tfrac1n04 (Batz et al., 8 Sep 2025).

The key invariant is formulated over reachable subdistributions 1n\tfrac1n05. For some fixed 1n\tfrac1n06,

1n\tfrac1n07

and for all 1n\tfrac1n08,

1n\tfrac1n09

In words, 1n\tfrac1n10 remains fixed, 1n\tfrac1n11 stays in the interval 1n\tfrac1n12, 1n\tfrac1n13 remains in range, and conditioned on the current 1n\tfrac1n14, the candidate is uniformly distributed on 1n\tfrac1n15. The precondition is a Dirac distribution at 1n\tfrac1n16, and the postcondition is that the final value satisfies 1n\tfrac1n17 (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 1n\tfrac1n18, 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.

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Fast Dice Roller.