I'm glad you discovered something cool!
But now I'm going to rant.
That article is fundamentally wrong on a number of salient points.
Two things in particular bother me.
Fouad promulgates the use of remainder (%) to "scale" the number returned from
rand() (even though modulo/remainder has nothing to do with scaling). And yes, I know it is how people are regularly taught to handle numbers from
rand(), but it is incorrect. It introduces unacceptable bias. (See my link below for more.)
But the most egregious problem is that he goes to the effort of creating a histogram of the RNG to probe the distribution, which he then (mis)uses to
state the exact opposite of the result:
Fouad wrote: |
---|
To show that these numbers occur approximately with equal likelihood, let’s simulate 6000 rolls of a die with the program above so we should say that each number from 1 to 6 should appear approximatly 1000 times.
--snip code--
Face Frequency
1 980
2 993
3 1030
4 1009
5 1002
6 986 |
So we see that each face was chosen nearly 1000 times. |
sic, bold and italic emphases added
What!?
No! We see that faces 3, 4, and 5 were
significantly more likely to be chosen than 1, 2, and 6. That's what you call a loaded die, and people have literally been shot for playing with one.
(If the distribution were actually equal, you'd see a table of 1000s, plus or minus 1 for
rand()'s crudeness. I think Fouad realized that the results weren't as neat as he would have liked, so he used the word "approximately" to make it feel more acceptable, without actually understanding the math he was stomping all over.)
Don't get me wrong. I'm not beating up on Fouad, but he has presented an article with bad misinformation, and there's nothing I can do to flag it as such. Meaning, well-intentioned visitors to cplusplus.com are going to find a nice 4/5 rated article that teaches them bad stuff.
If you really want to learn more about playing with rand(), read the FAQ:
http://www.cplusplus.com/faq/beginners/random-numbers/
(Part two of the FAQ, how-to get random numbers with C++, TRNGs and CSPRNGs, is yet to be written. But what is there is enough to get started with the
important principles you must understand about PRNGs.)
Hope this helps.