Skip to content

Replace STL normal_distribution with Boost.Random normal_distribution

Simon Spannagel requested to merge mt_distributions into master

This MR replaces the std::normal_distribution used in several places of the code base with a thin wrapper allpix::normal_distribution which simply forwards to boost::normal_distribution from the Boost.Random library. The latter is therefore a new framework dependency.

The problem we were facing with regards to reproducibility of simulation results in different platforms result from the way <random> is defined in the C++ standard. The PRNGs themselves have a mandated implementation, which means they produce the same sequence of pseudo-random numbers on all supported platforms. The distributions (e.g. the normal distribution in question) however are not mandated and each platform implements them differently - resulting in different draws from the underlying distribution with the same pseudo-random number. A very good blog post summarizing the situation (and other issues with <random>) can be found here.

The only remedy for this is to either switch to a completely different random number library, e.g. Boost.Random, or to roll our own. Since we are only using a single distribution up till now, I have decided to just give it a try and to take the std::normal_distribution implementation from libstdc++, to simplify it and to place it in our code base. Seems to work pretty well.

I have also considered switching to a different library, e.g. using ROOT's TRandom, but this would require us to rework the whole random number distribution scheme in a multi-threaded environment, something we have just invested significant resources to get right (and have achieved that).

After a long-ish survey of available 3rd-party libraries and implementations (ROOT::TRandom, CLHEP/Geant4, PCG, ...) I found that the Boost.Random implementation is the nicest match. The package should be available everywhere easily and its interface is fully STL-compatible.

Edited by Simon Spannagel

Merge request reports