18 #include "util/timespan.hpp" 30 template <
typename Engine>
59 bool roll(
double chance );
62 double range(
double min,
double max );
65 template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
67 return static_cast<T
>(
range(static_cast<double>(min), static_cast<double>(max)));
71 template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
73 return range<T>( T{}, max );
77 double gauss(
double mean,
double stddev,
bool truncate_low_end =
false );
83 double exgauss(
double gauss_mean,
double gauss_stddev,
double exp_nu );
97 double gauss_pair_value = 0.0;
98 bool gauss_pair_use =
false;
102 template <
typename Engine>
105 const uint64_t s = engine.next();
112 template <
typename Engine>
115 gauss_pair_value = 0;
116 gauss_pair_use =
false;
124 template <
typename Engine>
128 uint64_t ui64 = engine.next();
129 ui64 &= 0x000fffffffffffff;
130 ui64 |= 0x3ff0000000000000;
131 union { uint64_t ui64;
double d; } u;
137 template <
typename Engine>
140 if ( chance <= 0 )
return false;
141 if ( chance >= 1 )
return true;
142 return real() < chance;
146 template <
typename Engine>
149 assert( min <= max );
150 return min +
real() * ( max - min );
164 template <
typename Engine>
167 assert(stddev >= 0 &&
"Calling gauss with negative stddev");
173 if ( gauss_pair_use )
175 z = gauss_pair_value;
176 gauss_pair_use =
false;
180 double x1, x2, w, y1, y2;
183 x1 = 2.0 *
real() - 1.0;
184 x2 = 2.0 *
real() - 1.0;
185 w = x1 * x1 + x2 * x2;
187 while ( w >= 1.0 || w == 0.0 );
189 w = std::sqrt( ( -2.0 * std::log( w ) ) / w );
194 gauss_pair_value = y2;
195 gauss_pair_use =
true;
201 double result = mean + z * stddev;
204 if ( truncate_low_end && result < 0 )
211 template <
typename Engine>
215 do { x =
real(); }
while ( x >= 1.0 );
216 return - std::log( 1 - x ) * nu;
220 template <
typename Engine>
224 return v > 0.0 ? v : 0.0;
228 template <
typename Engine>
231 return timespan_t::from_native(
range( timespan_t::to_native( min ), timespan_t::to_native( max ) ) );
235 template <
typename Engine>
238 return timespan_t::from_native(
gauss( static_cast<double>( timespan_t::to_native( mean ) ),
239 static_cast<double>( timespan_t::to_native( stddev ) ) ) );
243 template <
typename Engine>
246 return timespan_t::from_native(
exgauss( static_cast<double>( timespan_t::to_native( mean ) ),
247 static_cast<double>( timespan_t::to_native( stddev ) ),
248 static_cast<double>( timespan_t::to_native( nu ) ) ) );
261 uint64_t next() noexcept;
262 void seed( uint64_t start ) noexcept;
263 const char*
name()
const noexcept;
265 std::array<uint64_t, 2> s;
280 uint64_t next() noexcept;
281 void seed( uint64_t start ) noexcept;
282 const char*
name()
const noexcept;
284 std::array<uint64_t, 4> s;
295 uint64_t next() noexcept;
296 void seed( uint64_t start ) noexcept;
297 const char*
name()
const noexcept;
299 std::array<uint64_t, 16> s;
double exponential(double nu)
Exponential Distribution.
Definition: rng.hpp:212
RNG Engines.
Definition: rng.hpp:259
void reset()
Reset any state.
Definition: rng.hpp:113
double gauss(double mean, double stddev, bool truncate_low_end=false)
Gaussian Distribution.
Definition: rng.hpp:165
xoshiro256+ Random Number Generator
Definition: rng.hpp:278
const char * name() const
Return engine name.
Definition: rng.hpp:40
double range(double min, double max)
Uniform distribution in the range [min..max)
Definition: rng.hpp:147
Random number generator wrapper around an rng engine.
Definition: rng.hpp:31
void seed(uint64_t s)
Seed rng engine.
Definition: rng.hpp:45
Class for representing InGame time.
Definition: timespan.hpp:37
double exgauss(double gauss_mean, double gauss_stddev, double exp_nu)
Exponentially Modified Gaussian Distribution.
Definition: rng.hpp:221
bool roll(double chance)
Bernoulli Distribution.
Definition: rng.hpp:138
T range(T max)
Uniform distribution in the range [0..max)
Definition: rng.hpp:72
Random number generation.
Definition: action.hpp:37
uint64_t reseed()
Reseed using current state.
Definition: rng.hpp:103
XORSHIFT-1024 Random Number Generator.
Definition: rng.hpp:293
double stdnormal_cdf(double u)
The standard normal CDF, for one random variable.
Definition: rng.cpp:162
double real()
Uniform distribution in range [0..1)
Definition: rng.hpp:125
double stdnormal_inv(double p)
The inverse standard normal distribution.
Definition: rng.cpp:259
T range(T min, T max)
Uniform distribution in the range [min..max)
Definition: rng.hpp:66