-1

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?

Harry Budd
  • 17
  • 4
  • I can't reproduce your problem with the code you posted. However this answer may be useful to you: https://stackoverflow.com/a/44872822/3807729 It uses `thread_local static` to make the random generator persistent and thread-safe. – Galik Jul 05 '17 at 21:55
  • @Galik This helped tremendously, thank you! – Harry Budd Jul 05 '17 at 22:03
  • I am developing an *in need of testing* header-only library for this which you may or may not find useful. The relevant headers are `hol/random_numbers.h` and `hol/random_distros.h`. The relevant function would be `random_normal()`. The repository is here: https://github.com/galik/header-only – Galik Jul 05 '17 at 22:12

1 Answers1

2

The problem is that you need to recycle the random_device.

If you really want to keep the exact same function signature, the easiest way would be to use a global variable:

random_device rd;
mt19937 gen(rd());

double r_norm(double mean, double sigma){
    normal_distribution<double> d(mean, sigma);
    return d(gen);
}

That being said: stl random distributions are statefull, so you need to also recycle d if you want an actual valid distribution.

At this point, maintaining your interface would require a static std::map<pair<double, double>, normal_distribution> so that you recycle them properly as well.

  • This worked, but I decided to go with @Galik 's solution to avoid the use of global variables. Thank you for the help, though! – Harry Budd Jul 05 '17 at 22:04