I am trying to generate random numbers in C++-11 using boost. I have seen some nice posts about this and I was able to generate random numbers putting all my code in the main function, like suggested in this webpage (http://www.sitmo.com/article/generating-random-numbers-in-c-with-boost/).
However, I was not able to encapsulate a random number generator (RNG) that can be called by different functions. I would like this encapsulated RNG to receive as parameters the range of numbers and a random seed. I tried to do it by following the suggestions of this post (Boost random number generator).
I implemented the code below. But it fails in both: the change of random seed and the definition of the range of values.
#include <boost/random/variate_generator.hpp>
#include <boost/generator_iterator.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
typedef boost::mt19937 ENG; // Mersenne Twister
typedef boost::normal_distribution<double> DIST; // Normal Distribution
typedef boost::variate_generator<ENG,DIST> GEN; // Variate generator
class RNG {
private:
ENG eng;
DIST dist;
public:
GEN gen;
RNG(double min,double max,int seed)
: dist(min,max), gen(eng,dist)
{eng.seed(seed); }
};
And this is my main function.
int main(int argc, char **argv){
if(argc == 2){
int myseed = atoi(argv[1]);
double minimum = 1.;
double maximum = 10.;
int N = 20;
RNG myrn(minimum,maximum,myseed);
for (int i = 0; i < N; i++){
std::cout << myrn.gen() << std::endl;
}
return 0;
}
exit(1);
}
This implementation generates the same sequence of random numbers independently of the seed I use. Also, the random numbers are not in the range between minimum and maximum. Does any of you know how to fix these two issues?