Generating a random number of higher range

186 Views Asked by At

I had a discussion with my friend about writing a function (a computer program) which generates 9 values randomly using a random number generator which generates 4 values, i.e I have a PRNG rand(4) {1,2,3,4} , I want to use that to write a PRNG rand(9) {1,2,3,4,5,6,7,8,9}.

My friend suggested that take three instances of rand(4),

where the first rand1(4) is mapped to 0, 9/4/, 9/2, (3/4)x9
the second instance rand2(4) is mapped to 1/4th of the above values
the third instance rand3(4) is mapped to 1/4th of the rand2(4) generator.

Finally, rand(9) is given by round( rand1(4)+rand2(4)+rand3(4) )

How accurate is this?, does the above method truly yield a uniform distribution ?(ignoring the fact that it is a PRNG)

2

There are 2 best solutions below

0
On

This doesn't precisely answer your question, but an alternative approach would be to use $(\mathrm{rand}(4)-1)+4(\mathrm{rand}(4)-1)+1$ and through out any values greater than $9$, which would give you a uniform distribution.

0
On

Your method essentially generates (assuming subsequent calls to rand(4) are independent) the number $\frac{9}{64}x$ with $x\in\{0,\ldots,63\}$ rounded (up? down?). Since there are $64>7\cdot 9$ equally likely values of $x$, the rounding will produce at least one value $\in\{0,\ldots,8\}$ for which there are at least $8$ values of $x$ producing it. So that outcome has a probability of at least $\frac18>\frac19$. A rejection startegy as proposed by Alex Becker can helkp you out of this.