24#ifndef DUMUX_COMMON_RANDOM_HH
25#define DUMUX_COMMON_RANDOM_HH
40template<
class Scalar =
double>
45 Parameters(Scalar
a, Scalar
b)
48 Scalar
a()
const {
return a_; }
49 Scalar
b()
const {
return b_; }
77 {
return { offset_, offset_+size_ }; }
79 Scalar
a()
const {
return offset_; }
80 Scalar
b()
const {
return offset_ + size_; }
82 template<
class Generator,
83 typename std::enable_if_t<std::is_same_v<typename Generator::result_type, std::uint_fast32_t>,
int> = 0>
85 {
return offset_ + size_*(0x1.0p-32 * gen()); }
99template<
class Scalar =
double>
104 Parameters(Scalar
m, Scalar
s)
107 Scalar
m()
const {
return m_; }
108 Scalar
s()
const {
return s_; }
136 {
return { mean_, stddev_ }; }
138 Scalar
m()
const {
return mean_; }
139 Scalar
s()
const {
return stddev_; }
141 template<
class Generator>
151 const auto [u1, u2] = generateUniformPair_(gen);
153 using std::sqrt;
using std::log;
154 using std::cos;
using std::sin;
155 constexpr Scalar twoPi = 2.0 * M_PI;
157 const Scalar magnitude = stddev_ * sqrt(-2.0 * log(u1));
158 const Scalar z0 = magnitude * cos(twoPi * u2) + mean_;
159 const Scalar z1 = magnitude * sin(twoPi * u2) + mean_;
166 template<
class Generator,
167 typename std::enable_if_t<std::is_same_v<typename Generator::result_type, std::uint_fast32_t>,
int> = 0>
168 auto generateUniformPair_(Generator& gen)
172 constexpr Scalar eps = std::numeric_limits<Scalar>::epsilon();
173 Scalar u1 = 0.0, u2 = 0.0;
175 u1 = 0x1.0p-32 * gen();
176 u2 = 0x1.0p-32 * gen();
178 return std::make_pair(u1, u2);
183 bool isCached_ =
false;
184 Scalar cachedValue_ = {};
193template<
class Scalar =
double>
202 : normal_(mean, stddev)
214 { normal_.param(p); }
217 {
return normal_.param(); }
219 Scalar
m()
const {
return normal_.m(); }
220 Scalar
s()
const {
return normal_.s(); }
222 template<
class Generator>
226 return exp(normal_(gen));
A simple uniform distribution based on a biased uniform number generator.
Definition: random.hh:42
Scalar b() const
Definition: random.hh:80
Scalar a() const
Definition: random.hh:79
SimpleUniformDistribution()
Definition: random.hh:66
void param(const Parameters &p)
Definition: random.hh:70
SimpleUniformDistribution(Scalar min, Scalar max=1.0)
Definition: random.hh:57
SimpleUniformDistribution(const Parameters &p)
Definition: random.hh:62
Parameters param() const
Definition: random.hh:76
Scalar operator()(Generator &gen)
Definition: random.hh:84
Parameters param_type
Definition: random.hh:54
Scalar result_type
Definition: random.hh:55
A simple normal distribution based on a biased uniform number generator and the Box-Mueller transform...
Definition: random.hh:101
Scalar operator()(Generator &gen)
Definition: random.hh:142
Parameters param_type
Definition: random.hh:113
void param(const Parameters &p)
Definition: random.hh:129
SimpleNormalDistribution(const Parameters &p)
Definition: random.hh:121
SimpleNormalDistribution(Scalar mean, Scalar stddev=1.0)
Definition: random.hh:116
Scalar result_type
Definition: random.hh:114
Parameters param() const
Definition: random.hh:135
Scalar m() const
Definition: random.hh:138
SimpleNormalDistribution()
Definition: random.hh:125
Scalar s() const
Definition: random.hh:139
A simple log-normal distribution.
Definition: random.hh:195
Scalar operator()(Generator &gen)
Definition: random.hh:223
Scalar m() const
Definition: random.hh:219
SimpleLogNormalDistribution()
Definition: random.hh:209
Parameters param_type
Definition: random.hh:198
Parameters param() const
Definition: random.hh:216
SimpleLogNormalDistribution(const Parameters &p)
Definition: random.hh:205
SimpleLogNormalDistribution(Scalar mean, Scalar stddev=1.0)
Definition: random.hh:201
void param(const Parameters &p)
Definition: random.hh:213
Scalar s() const
Definition: random.hh:220
Scalar result_type
Definition: random.hh:199