I have a function rand_double() which generates a random floating-point value using a static function variable with type std::mt19937. Several threads may use this function concurrently.
#include <random>
auto random_double(double min, double max) {
static std::mt19937 generator{std::random_device{}()}; // Forgot to seed it, thanks @user4581301
return std::uniform_real_distribution<>{min, max}(generator);
}
My first question is, is the above code as-is thread safe? I know that since C++11 the initialization of static function variables is always thread-safe, but how about the concurrent use of generator?
To generate a random number, the
std::uniform_real_distributioncallsoperator()of the argument generator.In case of mersenne twister the operator has the side effect:
which is not (by any indicator) considered a thread-safe operation.
You can use it as
thread_localwhich would have the intended effect i.e.staticin each specific thread: