Using this basic boost example on boost’s website if I have code like this:
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/uniform_real.hpp>
boost::mt19937 gen;
int roll_die() {
boost::uniform_real<> dist(1, 6);
boost::variate_generator<boost::mt19937&, boost::uniform_real<> > die(gen, dist);
return die();
}
If I wanted to do something like
boost::uniform_real<> dist(0.0, 6.0)
It give me something like 1.4883 for example. Is there a way to get it to give me only values increment of 0.5.
Example: (0.0, 6.0)
If I ran the code 6 times it would give me something like this:
0.5,
1.5,
5.5,
6.0,
3.5,
3.0
How would I get it to do that?
Thanks.
There are two ways to do this:
The simple way is to get a random integer from double the range (the closed range
[0, 12]) and divide by 2.0.The more difficult way is to get a random real from the range extended out by half the unit (the open range
(-0.25, 6.25)) and round to the nearest 0.5.The latter is more difficult because you’re going to guess the range wrong quite often, and it’s hard to write tests to make sure you got it right. (In fact, I’m not absolutely positive the right range is
(-0.25, 6.25)rather than[-0.25, 6.25), at least for IEEE floats… And, if that is the right range,boost::random::uniform_real_distributiondoesn’t give you any way to get it…)