Generate Uniform Distribution

How can you generate a uniformly distributed random number between 1 and 7 using only one die?

Answer

The general strategy is as follows:

  • Roll the die twice, yielding 36 equally likely outcomes.

  • The closest multiple of 7 less than or equal to 36 is 35. Map 35 of these outcomes evenly to the numbers 1 through 7 (each number gets 5 outcomes).

  • If the outcome is the extra one (for example, the outcome corresponding to 36, such as (6,6)), discard it and roll again.

We want to minimize the EV of rolling dices in order to simulate the above probabilities, hence we try to remove as little outcomes as possible. In this case the expected number of coin tosses is

\[ 2 \times \frac{36}{35}, \]

which is very close to 2 tosses.

Notes and comments

Comment 1: In some cases you may be required to generate a uniform distribution over a different range using a fixed number \(N\) of rolls. In that case, you should choose \(N\) such that \(6^N\) is divisible by the desired number \(M\) (for example, if \(M=8\)). In our case, since \(M=7\) does not evenly divide \(6^2 = 36\), we use rejection sampling.

Back to collection