I'm using the following function to generate gaussian random numbers:
double r_norm(double mean, double sigma){
random_device rd;
mt19937 gen(rd());
normal_distribution<double> d(mean, sigma);
return d(gen);
}
However, when I call this in main() with cout:
for (int k = 0; k < 10; k++){
cout << r_norm(2,0.5) <<endl;
}
It outputs the same number 10 times. Ideally I need to be able to call this function wherever, in order to receive a newly generated number each time.
Update: I managed to fix this by declaring the random device and mersenne twister outside of scope as global variables, but is there a neater way to do this?