I'm doing a Monte Carlo simulation. I have a derived class that is using some randomness from a standard normal distribution in one of its methods. The relevant .cpp file portion is something like:
double MyClass::foo(double x) {
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<double> dis(0.0,1.0);
double random = dis(gen);
double y= random * x;
return y;
}
where x
is some class property. This function works fine but is very slow when running the simulation. I believe that this is because I am setting up a new random device every time this function is called. Ideally, I would like to initialise and seed the random device once in the class as a property and then simply called it in foo
to generate random numbers. I have not figured out how to do this though and can't find any easy to follow or directly applicable questions that have already been asked.
Could anyone please advise?
Aucun commentaire:
Enregistrer un commentaire