2

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

  • 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?) http://math.stackexchange.com/questions/1273214/is-it-possible-to-create-a-completely-random-integer-between-1-and-13-using-stan, http://math.stackexchange.com/questions/1314460/how-to-generate-a-random-number-between-1-and-10-with-a-six-sided-die?lq=1 – 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
  • Yes, I forgot some exponents. What I wrote originally definitely is not uniform(0, 1). I meant $(X_1 - 1) / 6 + (X_2 - 1) / 6^2 + (X_3 - 1) / 6^3 + \ldots$. Think of writing a fraction in base six where all digits are equally likely. – dsaxton Jun 25 '15 at 20:57
  • @dsaxton what's the correct expression? Or is it another way to write the "mod 7" technique in the answers? – shadowtalker Jun 25 '15 at 20:58

2 Answers2

4

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.

Thomas Andrews
  • 177,126
4

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.

Ashkay
  • 1,821