0

I need pseudo-random floats to generate a sampling pattern. However, whenever I run the app with the same parameters, I want to get exactly the same pattern, so I want to use a fixed seed. I am generating these samples in multiple threads simultaneously. unfortunately std::srand() is not thread-safe and whether std::rand() is thread-safe depends on the implementation.

how can I generate random numbers with fixed seed per thread in c++ in a thread-safe manner?

matthias_buehlmann
  • 4,641
  • 6
  • 34
  • 76
  • You can bang out a thread safe `std::rand` in a few lines of code. (All you need to do is pass the last drawn value as an input to the LGC). Do check that the generator is adequate statistically for your application. – Bathsheba Aug 02 '21 at 14:45
  • Pass the seed to the threads and have each thread create its own [PRNG object](https://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine) with it. – NathanOliver Aug 02 '21 at 14:46
  • 1
    The accepted answer in the duplicate is superior to my comment. (I still wish you could upvote a good duplicate find). – Bathsheba Aug 02 '21 at 14:50

0 Answers0