I'm looking for a set of portable distributions for the standard C++11 engines like `std::mt19937' (see http://en.cppreference.com/w/cpp/numeric/random).
The engine implementations perform consistently (i.e. same sequence generated on different platforms – tested with Clang and MSVC), but the distributions seem to be implemented differently on the different platforms.
So, even though the engines produce the same sequence, it seems that a distribution (for example, std::normal_distribution<double>
) does not use the same number of samples (i.e. produces different results) on the different platforms, which is not acceptable in my case.
Is there maybe a 3rd party lib I can use that follows the C++11 random templates, but that will deliver consistent values across popular platforms (Looking at support across GCC, MSVC and Clang/llvm).
Options I have looked at so far are:
- Boost.random (a bit heavy, but worthwhile since it matches the c++11 counterparts quite well)
- Cloning from libstd++ (also worthwhile and probably portable, but pulling out specific functions might not be straightforward)
- Creating my own C++11-like random distributions
I need uniform, normal, poison and Rayleigh.
I have created my own C++11 distributions:
The uniform distribution seems to deliver good results and the normal distribution delivers very good results:
100000 values -> 68.159% within 1 sigma; 95.437% within 2 sigma; 99.747% within 3 sigma
The normal distribution uses the Box-Muller method, which according to what I have read so far, is not the fastest method, but it runs more that fast enough for my application.
Both the uniform and normal distributions should work with any C++11 engine (tested with std::mt19937) and provides the same sequence on all platforms, which is exactly what I wanted.