jeudi 7 mai 2015

thread-local static uniform_int_distribution, will it be recreated for different sets of min, max?

In this answer on stackoverflow (answer #2) @remyabel recommends this template:

#include <random>

std::mt19937& prng_engine()
{
  thread_local static std::random_device rd{};
  thread_local static std::mt19937 engine{rd()};

  // Or you can replace the two previous lines with:
  //thread_local static std::mt19937
  //  prng{std::random_device{}()};

  return engine;
}

template<typename T>
T getRandomNumberBetween(T Min, T Max)
{
    thread_local static std::uniform_int_distribution<T> dist{Min, Max};
    return dist(prng_engine());
}

Will dist be created only once for give set of {T, Min, Max} in thread?

If I call it with (1, 10) and then with (11, 20) will it be recreated or not?




Aucun commentaire:

Enregistrer un commentaire