![]() |
Home | Libraries | People | FAQ | More |
For the source of this example see die.cpp.
First we include the headers we need for mt19937
and uniform_int_distribution
.
#include <boost/random/mersenne_twister.hpp> #include <boost/random/uniform_int_distribution.hpp>
We use mt19937
with the
default seed as a source of randomness. The numbers produced will be the
same every time the program is run. One common method to change this is to
seed with the current time (std::time(0)
defined in ctime).
boost::random::mt19937 gen;
![]() |
Note |
---|---|
We are using a global generator object here. This is important because we don't want to create a new pseudo-random number generator at every call |
Now we can define a function that simulates an ordinary six-sided die.
int roll_die() {boost::random::uniform_int_distribution<> dist(1, 6);
return dist(gen); }
|
||||
A distribution is a function object. We generate a random number by calling
|