Generating random numbers and letting the STL shape specific distributions
In the last recipe, we learned some bits about the STL random number engines. Generating random numbers this or the other way is often only half of the work.
Another question is, what do we need those numbers for? Are we programmatically "flipping a coin"? People used to do this using rand() % 2
, which results in values of 0
and 1
that can then be mapped to head or tail. Fair enough; we do not need a library for that (although randomness experts know that just using the lowest few bits of a random number does not always lead to high-quality random numbers).
What if we want to model a die? Then, we could surely write (rand() % 6) + 1
, in order to represent the result after rolling the die. There is still no pressing library needed for such simple tasks.
What if we want to model something that happens with an exact probability of 66%? Okay, then we can come up with a formula like bool yesno = (rand() % 100 > 66)
. (Oh...