FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
Random.h
1#pragma once
2
12#include <cmath>
13#include <cstdint>
14#include <numbers>
15#include <random>
16
17namespace environment::field::aggregation
18{
19
21class Random
22{
23public:
24 explicit Random(std::uint64_t seed)
25 : m_engine(seed)
26 {
27 }
28
30 double Uniform() { return static_cast<double>(m_engine() >> 11U) * (1.0 / 9007199254740992.0); }
31
33 double Uniform(double low, double high) { return low + (high - low) * Uniform(); }
34
36 double Normal()
37 {
38 const double u1 = 1.0 - Uniform(); // in (0, 1], so the logarithm is finite
39 const double u2 = Uniform();
40 return std::sqrt(-2.0 * std::log(u1)) * std::cos(2.0 * std::numbers::pi * u2);
41 }
42
44 double Normal(double mean, double sigma) { return mean + sigma * Normal(); }
45
47 double LogNormal(double mu, double sigma) { return std::exp(Normal(mu, sigma)); }
48
49private:
50 std::mt19937_64 m_engine;
51};
52
53} // namespace environment::field::aggregation
One random stream. Not thread-safe.
Definition Random.h:22
double LogNormal(double mu, double sigma)
exp(Normal(mu, sigma)).
Definition Random.h:47
double Uniform()
Uniform in [0, 1), 53 bits.
Definition Random.h:30
double Normal(double mean, double sigma)
Normal with a mean and a standard deviation.
Definition Random.h:44
double Uniform(double low, double high)
Uniform in [low, high).
Definition Random.h:33
double Normal()
Standard normal (Box-Muller, one value per two uniforms; the sine partner is discarded).
Definition Random.h:36