Orrery
A GPU-accelerated N-body gravitational simulator
Loading...
Searching...
No Matches
random.hpp File Reference

The project's source of randomness, and the only one. More...

#include <cstdint>
#include <limits>
#include <random>
#include "orrery/core/types.hpp"
#include "orrery/core/vec3.hpp"

Go to the source code of this file.

Classes

class  orrery::core::RandomSource
 A seeded stream of random numbers. More...

Detailed Description

The project's source of randomness, and the only one.

Every result Orrery reports about a sampled configuration is a claim about a specific set of particles. If that set cannot be reconstructed, the claim cannot be rechecked, and a regression test against a golden output is testing the generator as much as the physics. So the requirement is stronger than "random enough": the same seed must give the same particles, on every compiler and every platform the project builds on.

The standard library provides half of that. std::mt19937_64 is specified bit for bit by the standard, so it produces the same stream everywhere. The distributions are not: std::uniform_real_distribution is defined by its output distribution and not by its algorithm, and three standard libraries give three different sequences from one engine and one seed. Only the engine is therefore used, and the mapping from bits to numbers is written here. ADR-0009 records the alternatives.

One limit is worth stating rather than leaving to be discovered. The values this class returns are bit-identical across platforms, because they are made with shifts and an exact power-of-two multiplication. Quantities derived from them through the standard maths library, a square root or a cosine, are identical only as far as that library's last bit, which is not guaranteed to agree between platforms. Reproducibility of a sampled configuration is therefore exact within a platform and to a few units in the last place across them, which is why the tests over sampled configurations compare against tolerances rather than against stored values.