Skip to content

Instantly share code, notes, and snippets.

@srivathsanmurali
Created April 12, 2017 08:48
Show Gist options
  • Save srivathsanmurali/6ca74bdf154b0f5447321be183ba28d1 to your computer and use it in GitHub Desktop.
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.
#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});
}
@image357
Copy link

are you sure? calls to std::random_device utilize some global state which might not be thread safe.
https://stackoverflow.com/q/42157381/8088550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment