# Uniform distribution on $\{1,\dots,7\}$ from rolling a die [duplicate]

This question already has an answer here:

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).

## marked as duplicate by Micah, Community♦Jun 25 '15 at 20:42

• Hint: You can't do it in a limited number of rolls, because in $N$ rolls, you get $6^N$ different values, and that isn't divisible by $7$. – Thomas Andrews Jun 25 '15 at 20:36
• Other related questions (abstract duplicates?) math.stackexchange.com/questions/1273214/…, math.stackexchange.com/questions/1314460/… – Micah Jun 25 '15 at 20:42
• @Micah thanks, good finds all – shadowtalker Jun 25 '15 at 20:43
• So you're allowed to roll the die an infinite number of times? Then if $X_1, X_2, X_3, \ldots$ are the values of the different rolls take $(X_1 - 1) / 6 + (X_2 - 1) / 6 + (X_3 - 1) / 6 + \ldots \sim$ uniform$(0, 1)$ and you can generate from any distribution whatsoever. – dsaxton Jun 25 '15 at 20:50
• @dsaxton won't that be bell-shaped? Try it in R: x <- colMeans(replicate(500, sample(1:6, 100, replace = TRUE))); qqnorm(x). You could hack it by assuming normality and taking its inverse, u <- pnorm(x, mean(x), sd(x)), but that's... a hack. – shadowtalker Jun 25 '15 at 20:54

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.
You can't do it in a limited number of rolls, because in $N$ rolls, you get $6^N$ element 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$.