SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
randomstream.h
Go to the documentation of this file.
1// SedSAT3 – Sediment Source Assessment Tool, version 3
2// U.S. Geological Survey. See LICENSE.md and DISCLAIMER.md in the repository root.
3
4#pragma once
5
6#include <cmath>
7#include <cstdint>
8#include <random>
9#include <string>
10
27{
28public:
35 explicit RandomStream(std::uint64_t seed = 0, std::uint64_t stream = 0)
36 {
37 Reseed(seed, stream);
38 }
39
40 void Reseed(std::uint64_t seed, std::uint64_t stream = 0)
41 {
42 std::seed_seq sequence{
43 static_cast<std::uint32_t>(seed), static_cast<std::uint32_t>(seed >> 32),
44 static_cast<std::uint32_t>(stream), static_cast<std::uint32_t>(stream >> 32)};
45 engine_.seed(sequence);
46 has_spare_normal_ = false;
47 }
48
50 double Uniform()
51 {
52 return static_cast<double>(engine_() >> 11) * 0x1.0p-53;
53 }
54
57 {
58 return static_cast<double>((engine_() >> 11) + 1) * 0x1.0p-53;
59 }
60
63 {
65 {
66 has_spare_normal_ = false;
67 return spare_normal_;
68 }
69 const double radius = std::sqrt(-2.0 * std::log(UniformPositive()));
70 const double angle = 6.283185307179586476925 * Uniform();
71 spare_normal_ = radius * std::sin(angle);
72 has_spare_normal_ = true;
73 return radius * std::cos(angle);
74 }
75
77 double Normal(double mean, double stdev)
78 {
79 return mean + stdev * StandardNormal();
80 }
81
83 std::uint64_t Index(std::uint64_t n)
84 {
85 return static_cast<std::uint64_t>(Uniform() * static_cast<double>(n)) % n;
86 }
87
88private:
89 std::mt19937_64 engine_;
90 double spare_normal_ = 0.0;
91 bool has_spare_normal_ = false;
92};
93
100inline std::uint64_t DrawRandomSeed()
101{
102 std::random_device device;
103 return static_cast<std::uint64_t>(device());
104}
105
112inline bool ParseRandomSeed(const std::string& text, std::uint64_t& seed)
113{
114 std::size_t begin = text.find_first_not_of(" \t");
115 if (begin == std::string::npos)
116 {
117 seed = DrawRandomSeed();
118 return true;
119 }
120 std::size_t end = text.find_last_not_of(" \t");
121 const std::string digits = text.substr(begin, end - begin + 1);
122 if (digits.find_first_not_of("0123456789") != std::string::npos || digits.size() > 20)
123 return false;
124 try
125 {
126 std::size_t used = 0;
127 const unsigned long long value = std::stoull(digits, &used, 10);
128 if (used != digits.size())
129 return false;
130 seed = static_cast<std::uint64_t>(value);
131 return true;
132 }
133 catch (const std::exception&)
134 {
135 return false;
136 }
137}
138
150constexpr std::uint64_t DefaultRandomSeed = 20261016;
151
153{
154 thread_local RandomStream stream(DefaultRandomSeed);
155 return stream;
156}
A seeded, independent stream of random numbers.
double spare_normal_
double Normal(double mean, double stdev)
Normal with mean mean and standard deviation stdev.
std::mt19937_64 engine_
RandomStream(std::uint64_t seed=0, std::uint64_t stream=0)
Stream number stream derived from seed.
double UniformPositive()
Uniform on (0, 1], never zero, for taking logarithms.
double Uniform()
Uniform on [0, 1), with 53 random bits.
std::uint64_t Index(std::uint64_t n)
Uniform integer on [0, n), n > 0.
void Reseed(std::uint64_t seed, std::uint64_t stream=0)
double StandardNormal()
Standard normal, by the Box-Muller transform.
bool has_spare_normal_
bool ParseRandomSeed(const std::string &text, std::uint64_t &seed)
Parses a user-entered seed.
constexpr std::uint64_t DefaultRandomSeed
The stream for code outside the MCMC that needs random numbers.
RandomStream & ThreadRandomStream()
std::uint64_t DrawRandomSeed()
A seed drawn from the operating system's entropy source.