algorithm - Given a true random number generator which outputs either a 1 or 0 per call, how do you use this to pick a number from an arbitrary range? -
if have true random number generator (trng) can give me either 0 or 1 each time call it, trivial generate number in range length equal power of 2. example, if wanted generate random number between 0 , 63, poll trng 5 times, maximum value of 11111 , minimum value of 00000. problem when want number in rangle not equal 2^n. wanted simulate roll of dice. need range between 1 , 6, equal weighting. clearly, need 3 bits store result, polling trng 3 times introduce 2 eroneous values. ignore them, give 1 side of dice lower odds of being rolled.
my question of ome deals this.
the easiest way accurate result rejection sampling. example, generate random value 1 8 (3 bits), rejecting , generating new value (3 new bits) whenever 7 or 8. in loop.
you can arbitrarily close accurate generating large number of bits, doing mod 6, , living bias. in cases 32-bit values mod 6, bias small impossible detect, after simulating millions of rolls.
Comments
Post a Comment