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?
Aucun commentaire:
Enregistrer un commentaire