Make random generator thread_local.

This commit is contained in:
Faerbit 2020-06-13 21:30:03 +02:00
parent bd704eb6ed
commit 5243882ed5

6
util.h
View File

@ -4,6 +4,7 @@
#include <limits> #include <limits>
#include <random> #include <random>
#include <ctime> #include <ctime>
#include <functional>
const double infinity = std::numeric_limits<double>::infinity(); const double infinity = std::numeric_limits<double>::infinity();
const double pi = 3.1415926535897932385; const double pi = 3.1415926535897932385;
@ -15,8 +16,9 @@ inline double degrees_to_radians(double degrees) {
inline double random_double() { inline double random_double() {
static std::uniform_real_distribution<double> distribution(0.0, 1.0); static std::uniform_real_distribution<double> distribution(0.0, 1.0);
//static std::mt19937 generator(std::time(nullptr)); //static std::mt19937 generator(std::time(nullptr));
static std::mt19937 generator(std::random_device{}()); static thread_local std::mt19937 generator(std::random_device{}());
return distribution(generator); static std::function<double()> rand_gen = std::bind(distribution, generator);
return rand_gen();
} }
inline double random_double(double min, double max) { inline double random_double(double min, double max) {