SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
RandomStream Class Reference

A seeded, independent stream of random numbers. More...

#include <randomstream.h>

Collaboration diagram for RandomStream:
Collaboration graph

Public Member Functions

 RandomStream (std::uint64_t seed=0, std::uint64_t stream=0)
 Stream number stream derived from seed.
void Reseed (std::uint64_t seed, std::uint64_t stream=0)
double Uniform ()
 Uniform on [0, 1), with 53 random bits.
double UniformPositive ()
 Uniform on (0, 1], never zero, for taking logarithms.
double StandardNormal ()
 Standard normal, by the Box-Muller transform.
double Normal (double mean, double stdev)
 Normal with mean mean and standard deviation stdev.
std::uint64_t Index (std::uint64_t n)
 Uniform integer on [0, n), n > 0.

Private Attributes

std::mt19937_64 engine_
double spare_normal_ = 0.0
bool has_spare_normal_ = false

Detailed Description

A seeded, independent stream of random numbers.

Every random number in SedSAT3 comes from a RandomStream. The engine is std::mt19937_64 seeded through std::seed_seq; both are fully specified by the C++ standard, and the uniform and normal values are computed here from the engine's raw output rather than with std::uniform_real_distribution and std::normal_distribution, whose algorithms differ between standard libraries. The same seed therefore gives the same numbers with GCC, Clang and MSVC, on Linux, macOS and Windows.

A stream is not thread-safe. Code that runs in parallel gives each chain or thread its own stream, seeded from a master seed and the stream's index, so results do not depend on how work is scheduled across threads.

Definition at line 26 of file randomstream.h.

Constructor & Destructor Documentation

◆ RandomStream()

RandomStream::RandomStream ( std::uint64_t seed = 0,
std::uint64_t stream = 0 )
inlineexplicit

Stream number stream derived from seed.

Different stream numbers with the same seed give statistically independent sequences.

Definition at line 35 of file randomstream.h.

References Reseed().

Member Function Documentation

◆ Index()

std::uint64_t RandomStream::Index ( std::uint64_t n)
inline

Uniform integer on [0, n), n > 0.

Definition at line 83 of file randomstream.h.

References Uniform().

◆ Normal()

double RandomStream::Normal ( double mean,
double stdev )
inline

Normal with mean mean and standard deviation stdev.

Definition at line 77 of file randomstream.h.

References StandardNormal().

◆ Reseed()

void RandomStream::Reseed ( std::uint64_t seed,
std::uint64_t stream = 0 )
inline

Definition at line 40 of file randomstream.h.

References engine_, and has_spare_normal_.

Referenced by RandomStream().

◆ StandardNormal()

double RandomStream::StandardNormal ( )
inline

◆ Uniform()

double RandomStream::Uniform ( )
inline

◆ UniformPositive()

double RandomStream::UniformPositive ( )
inline

Uniform on (0, 1], never zero, for taking logarithms.

Definition at line 56 of file randomstream.h.

References engine_.

Referenced by StandardNormal().

Member Data Documentation

◆ engine_

std::mt19937_64 RandomStream::engine_
private

Definition at line 89 of file randomstream.h.

Referenced by Reseed(), Uniform(), and UniformPositive().

◆ has_spare_normal_

bool RandomStream::has_spare_normal_ = false
private

Definition at line 91 of file randomstream.h.

Referenced by Reseed(), and StandardNormal().

◆ spare_normal_

double RandomStream::spare_normal_ = 0.0
private

Definition at line 90 of file randomstream.h.

Referenced by StandardNormal().


The documentation for this class was generated from the following file: