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 15 generator is pretty timeconsuming, so you want to minimize the number of times you are going to use it.
(In reply to
re(3): solution by friedlinguini)
If I'm interpreting your description correctly, the nonstatic version of your algorithm averages about 2.219 calls to rand5 for each base7 digit. My algorithm averages 1.266 calls to rand5 per base7 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 20030701 05:38:24 