File RandomDist.hpp
↰ Parent directory (src/utils)
Portable random number distributions.
Definition (src/utils/RandomDist.hpp)
Detailed Description
Mark Krumholz
2026-10-13
Copyright (c) 2026 Mark Krumholz. All rights reserved.
The C++ standard specifies random number engines exactly, but leaves the algorithms that the standard distributions (std::normal_distribution and friends) use to turn engine output into deviates up to the implementation. As a result the same engine state yields different deviates under different standard libraries (e.g., libstdc++ versus libc++), so neither runs nor clusters reconstructed from a stored rng state (see slug_reader.get_cluster) are reproducible across compilers.
The classes and functions here replace the standard distributions with slug’s own implementations. They deliberately reproduce, operation for operation, the algorithms used by GCC 14’s libstdc++, so that every build of slug now generates the same deviates that a libstdc++ build always has, and rng states recorded by earlier libstdc++ runs remain valid.
Bitwise agreement also requires that the compiler not contract expressions such as a * b + c into fused multiply-adds, which some compilers do by default on targets that have them (e.g., arm64), but libstdc++ builds for baseline x86_64 do not. Contraction is therefore switched off for this file’s code only: by pragmas scoped to each function body for Clang, and by an optimize pragma pushed and popped around this file’s definitions for GCC. The only remaining source of disagreement is last-bit differences between platforms’ implementations of log, exp, and lgamma, which can change the sequence of draws only in the very rare event that such a difference flips the outcome of a comparison.
Note that some of these distributions carry state between calls (e.g., the Marsaglia polar method generates normal deviates in pairs), so reproducing a sequence of draws also requires constructing and discarding distribution objects at the same points as the run being reproduced.
Includes
RngThread.hpp(File RngThread.hpp)algorithmcmathcstddefcstdintlimitsnumericspanstdexceptvector