Suppose you have a function (or a magic ball) that is capable of producing a totally random integer between 1 and 5 (inclusive).
Using this, how would you generate a random number (also an integer) between 1 and 7 (inclusive)? (Note that the for the number to be random, all integers between 1 and 7 must have an equal chance of being generated)
Assume that using your 1-5 generator is pretty time-consuming, so you want to minimize the number of times you are going to use it.
(In reply to re(3): solution
If I'm interpreting your description correctly, the non-static version of your algorithm averages about 2.219 calls to rand5 for each base-7 digit. My algorithm averages 1.266 calls to rand5 per base-7 digit given that a large number of them will be needed. I take it that applying STATIC to your technique will actually bring it down to the theoretical 1.209 calls per digit; is that correct? (that's log(7)/log(5)).
Posted by Charlie
on 2003-07-01 05:38:24