c++randomthread-safetyc++20mt19937

Is a `std::mt19937` static function variable thread-safe?


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?


Solution

  • To generate a random number, the std::uniform_real_distribution calls operator() of the argument generator.

    In case of mersenne twister the operator has the side effect:

    "The state of the engine is advanced by one position"

    which is not (by any indicator) considered a thread-safe operation.

    You can use it as thread_local which would have the intended effect i.e. static in each specific thread:

    auto random_double(double min, double max) {
        thread_local std::mt19937 generator(std::random_device{}());
        return std::uniform_real_distribution<>{min, max}(generator);
    }