![]() |
Home | Libraries | People | FAQ | More |
For the source of this example see weighted_die.cpp.
#include <boost/random/mersenne_twister.hpp> #include <boost/random/discrete_distribution.hpp> boost::mt19937 gen;
This time, instead of a fair die, the probability of rolling a 1 is 50% (!). The other five faces are all equally likely.
discrete_distribution
works nicely here by allowing us to assign weights to each of the possible
outcomes.
![]() |
Tip |
---|---|
If your compiler supports |
double probabilities[] = { 0.5, 0.1, 0.1, 0.1, 0.1, 0.1 }; boost::random::discrete_distribution<> dist(probabilities);
Now define a function that simulates rolling this die.
int roll_weighted_die() {return dist(gen) + 1; }