Expanding the precision or range of a random number generator

265 Views Asked by At

Suppose I have a random number generator that can produce a uniform distribution of natural numbers in the range $[1, 10]$. So there are 10 possible values the generator can produce.

Suppose I would like to generate natural numbers with a uniform distribution spanning $[1, 30]$. Is there a way to do that with my existing generator?

1

There are 1 best solutions below

1
On

For convenience, shift the ranges to [0,9] and [0,29].

Draw a first number A. Draw a second one, B; if 9, draw again until not 9.

Compute 3.A + B/3.


The reason to reject 9 is to ensure that B/3 is uniform with probabilities 1/3.


If I am right, you can also keep the remainder from one drawing to the next, add it to B (giving a uniform number in [0,11]) and divide by 4 instead of 3.

Initialize R
Loop
    Draw A
    Draw B
    Output 3.A + (B+R) / 4
    Keep R = (B+R) % 4