Uniform distribution on $\{1,\dots,7\}$ from rolling a die

1.6k Views Asked by At

This was a job interview question someone asked about on Reddit. (Link)

How can you generate a uniform distribution on $\{1,\dots,7\}$ using a fair die?

Presumably, you are to do this by combining repeated i.i.d draws from $\{1,\dots,6\}$ (and not some Rube-Goldberg-esque rig that involves using the die for something other than rolling it).

2

There are 2 best solutions below

0
On BEST ANSWER

You can't do it in a limited number of rolls, because in $N$ rolls, you get $6^N$ elements in the sample space, and that isn't divisible by $7$.

So roll the die twice, use those two values to encode a number from $1$ and $36$. If the first roll is $A$ and the second $B$, you can do this by computing $6A+B-6$.

If you get $36$, roll again.

If not, reduce modulo $7$, then add $1$.

Potentially, you could be rolling forever. In reality, it is highly unlikely you'd have to roll more than 6 times to get a value.

0
On

The easiest way: roll the dice twice. Now, there are 36 possible pairs. Delegate five pairs to each of 1, 2, 3, 4, 5, 6, 7 - then, if you get the remaining pair, roll the dice again. This guarantees equal probability for each of the 7 values. Note that the probability you don't have to roll again is $35/36$, so the expected value of dice you'll need to roll is pretty close to two.