r/dailyprogrammer • • Apr 03 '12

[4/3/2012] Challenge #35 [intermediate]

Imagine you are given a function flip() that returns a random bit (0 or 1 with equal probability). Write a program that uses flip to generate a random number in the range 0...N-1 such that each of the N numbers is generated with equal probability. For instance, if your program is run with N = 6, then it will generate the number 0, 1, 2, 3, 4, or 5 with equal probability.

N can be any integer >= 2.

Pseudocode is okay.

You're not allowed to use rand or anything that maintains state other than flip.

Thanks to Cosmologicon for posting this challenge to /r/dailyprogrammer_ideas!

13 Upvotes

24 comments sorted by

View all comments

Show parent comment

2

u/Steve132 0 1 Apr 03 '12

Correct me if I'm wrong, but I don't think this is actually evenly distributed. It looks like what you are doing is generating a 32 bit binary rand(), then returning rand() % n.

The problem with the rand() % n approach, is that it actually produces numbers that aren't randomly distributed if n isn't a power of two. Here is why: Consider we did the same thing, but with a 4 bit rand() instead. Then, you generated randomly a number between 0 and 15. We want to make it actually between 0 and 9, so we do % 9. Lets say we start with a perfectly uniform distribution: of samples

1 0 15 9 4 8 11 2 12 13 7 6 3 5 10 14 

Now, lets do mod 9

1 0 6 0 4 8 2 2 3 4 7 6 3 5 1 5

If we look at the distribution sorted now, we can clearly see that the numbers between 0 and (16-9) are duplicated.

0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 8

This is true in general...using rand() % n, if b is the closest power of two to n, then numbers between 0 and (b-n) are twice as likely to occur, because of the wrap-around effect of mod()

1

u/luxgladius 0 0 Apr 03 '12

That's correct in general. However, in order to dodge this, I implemented a limit on the result to ensure I get an even number of intervals. That is the expression $lim = int((232 )/$n)*$n -1. If I get a result larger than this number, then I am in the final interval that has been cut off which would, as you say, skew the probability toward the early members of the sequence. That is why if I get any result greater than the limit, I just generate a new one. Having such a big basis (232) means that I won't to have to "re-roll" very often for common values of $n, but it could happen.