Quant Memo
Foundational

Simulating a Die With a Coin and Back Again

How to generate a fair six-sided die roll using only coin flips (and a fair coin flip using only a die), plus the general principle — rejection sampling — that makes both constructions work.

Question. You have only a fair coin. Can you use it to simulate a fair roll of a six-sided die — each outcome exactly 1/61/6? And conversely, if you only had a fair die, could you simulate a fair coin flip? Both are yes, and the technique that makes them work — rejection sampling — is one of the most broadly useful ideas in this whole family of questions, showing up again in Monte Carlo methods and in generating custom-distributed random numbers in code.

Simulating a die with a coin

Flip the coin 3 times. Each sequence of 3 flips (H or T) gives one of 23=82^3 = 8 equally likely outcomes, which you can read as a binary number from 0 to 7. Map outcomes 0 through 5 to die faces 1 through 6. If you get outcome 6 or 7 (i.e., HHH or... more precisely two of the eight patterns), discard the result and flip again from scratch. Since outcomes 0–5 are each equally likely (each has probability 1/81/8) and you only ever accept when you land in that set, conditional on acceptance every one of the 6 outcomes is exactly equally likely — 1/61/6 each. This is rejection sampling: oversample from an easy-to-generate uniform set, discard the excess, and what remains is uniform over exactly the range you want.

1 2 3 4 5 6 reject reject Eight equally likely 3-flip outcomes: six accepted as die faces, two rejected and re-flipped
Rejection sampling in one picture: 6 of the 8 equally likely coin-flip patterns map onto the die's 6 faces one-to-one; the other 2 are discarded and the flips are retried.

Worked example: efficiency of the coin-to-die method

What fraction of attempts get rejected? 22 out of 88 patterns are rejected, so P(reject)=1/4P(\text{reject}) = 1/4, meaning you succeed on any given batch of 3 flips with probability 3/43/4. The expected number of batches (each 3 flips) needed follows a geometric distribution with success probability 3/43/4:

E[batches]=13/4=43.E[\text{batches}] = \frac{1}{3/4} = \frac{4}{3} .

So on average you need 43×3=4\frac{4}{3} \times 3 = 4 individual coin flips to produce one fair die roll — noticeably more efficient than a naive approach that might flip repeatedly hoping for a specific pattern with no rejection logic at all.

The reverse: simulating a coin with a die

This direction is easier. Roll the die once. If it shows 1, 2, or 3, call it "heads"; if 4, 5, or 6, call it "tails" — no rejection needed at all, since the die's six faces split evenly into two groups of three. If instead you only wanted to use some faces (say the die were unevenly weighted, or you wanted odd numbers as heads and one specific even number discarded), you'd fall back to the same rejection idea: accept only outcomes in a subset that splits evenly, and re-roll on anything outside it.

The general principle

Both directions rely on the same idea: take a source of randomness with mm equally likely outcomes, and you want to produce a target with nn equally likely outcomes. If nn divides evenly into some power or multiple of mm, you can partition the source's outcome space into nn equal-sized groups (direct mapping, no waste) or, if it doesn't divide evenly, into nn equal-sized groups plus a leftover "reject and retry" group (rejection sampling). This generalizes to simulating a fair 7-sided die from coin flips, a biased coin from a fair one, or arbitrary discrete distributions from a uniform random number generator in code — always the same partition-and-reject pattern.

Rejection sampling generates a target uniform distribution from a source uniform distribution by mapping source outcomes to target outcomes in equal-sized groups and discarding (re-sampling) any leftover outcomes that don't fit evenly. Simulating a die from 3 coin flips uses 6 of the 8 equally likely patterns and re-flips on the other 2, needing 4 flips on average.

When asked to simulate an nn-outcome fair process from an mm-outcome fair process, first check whether nn divides some power of mm cleanly (coin-to-die needs 23=862^3=8 \geq 6) — the smallest power that works minimizes wasted, rejected draws and is usually what the interviewer wants you to identify.

Related concepts

Practice in interviews

Further reading

  • Knuth & Yao, The Complexity of Nonuniform Random Number Generation (1976)
ShareTwitterLinkedIn