Created
April 12, 2017 08:48
-
-
Save srivathsanmurali/6ca74bdf154b0f5447321be183ba28d1 to your computer and use it in GitHub Desktop.
A simple random number generator that is thread safe using C++11 random engines. Random number generator engines are not thread safe. Using thread_local, initializes an engine per thread.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <random> | |
float randFloat(float low, float high) { | |
thread_local static std::random_device rd; | |
thread_local static std::mt19937 rng(rd()); | |
thread_local std::uniform_real_distribution<float> urd; | |
return urd(rng, decltype(urd)::param_type{low,high}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
are you sure? calls to std::random_device utilize some global state which might not be thread safe.
https://stackoverflow.com/q/42157381/8088550