Roll a die
This pattern is licensed under the CC0 Public Domain Dedication.
Generate a random integer according to a uniform distribution.
std::random_device on line 5
provides a source of uniform non-deterministic random numbers
(where such a source is available). We use this to seed the
Mersenne Twister random number engine,
line 6. Consider other random number
for different properties.
On line 7, we create a
representing a uniform random distribution of integers from 1 to 6
inclusive. That is, each value from 1 to 6 has an equal chance of
Finally, we generate a random number from the random engine, distributed according to the uniform integer distribution (line 9). This gives us a random integer from 1 to 6, as though we had rolled a 6-sided die.
To generate further random numbers, simply call
09 December 2017