How can I generate a bigger probability set from a smaller probability set?
This is from Algorithm Design Manual -Steven Skiena
Q:
Use a random number generator (rng04) that generates numbers from {0,1,2,3,4} with equal probability to write a random number generator that generates numbers from 0 to 7 (rng07) with equal probability?
I tried for around 3 hours now, mostly based on summing two rng04 outputs. The problem is that in that case the probability of each value is different – 4 can come with 5/24 probability while 0 happening is 1/24. I tried some ways to mask it, but cannot.
Can somebody solve this?
You have to find a way to combine the two sets of random numbers (the first and second random
{0,1,2,3,4}) and maken*ndistinct possibilities. Basically the problem is that with addition you get something like thisWhich has duplicates, which is not what you want. One possible way to combine the two sets would be the
Z = X + Y*5whereXandYare the two random numbers. That would give you a set of results like thisSo now that you have a bigger set of random numbers, you need to do the reverse and make it smaller. This set has
25distinct values (because you started with 5, and used two random numbers, so5*5=25). The set you want has 8 distinct values. A naïve way to do this would beThis would indeed have a range of
{0,7}. But the values{1,7}would appear 3/25 times, and the value0would appear 4/25 times. This is because0 mod 8 = 0,8 mod 8 = 0,16 mod 8 = 0and24 mod 8 = 0.To fix this, you can modify the code above to this.
This will take the one value (
24) that is throwing off your probabilities and discard it. Generating a new random number if you get a ‘bad’ value like this will make your algorithm run very slightly longer (in this case 1/25 of the time it will take 2x as long to run, 1/625 it will take 3x as long, etc). But it will give you the right probabilities.