A particular random number generator returns positive integer n with probability 1/(2^n). (ie '1' with probability 1/2, '2' with probability 1/4, '3' with probability 1/8, etc.)
Using this random number generator, write an algorithm which chooses a random integer from 1 to 37 with equal probability.
(In reply to
solution by Charlie)
For the record, the solution I posted averages 4 calls to the RNG for each random 164 produced. So when 27 of the 64 are thrown out the average grows to 4*64/37 = 6.9 RNG calls per number 137.
Brian Smith's is therefore the superior algorithm.
Edited on June 15, 2004, 10:34 am

Posted by Charlie
on 20040615 09:26:49 