Hey, thanks for the plug!
Bias Issue
There are more issues. Using
%
with
rand()
introduces
bias — and it is all due to the
Pigeonhole Principle|
https://en.wikipedia.org/wiki/Pigeonhole_principle
The basic idea is this: suppose rand() produced 10 possible numbers: 0..9. But you only want 7 numbers. So you tack on a
% 7 to get only 7 possible values.
The problem is that you are still getting
ten values.
0 7 →
0
1 8 →
1
2 9 →
2
3 →
3
4 →
4
5 →
5
6 →
6
See how you get two pigeons in cubbies 0, 1, and 2? That means that you are twice as likely to get a 0, 1, or 2 than, say, a 5.
So unless your RAND_MAX is a multiple of your target range (and it isn’t, since RAND_MAX is (2
n - 1)), you will always have a bias towards zero.
The proper way is to pull numbers and simply
discard those not in your target range. This is a little trickier than it sounds at first blush, because small ranges mean potential long waits... so you have to find a multiplier such that R*k < N, R*(k+1) > N, get a random number in R, and
then use modulo k.
Or you could just use a uniform_int_distribution to do it for you. :O)
Hope this helps.