|
| int64_t | generate_63bit_seed () |
| | Generates a seed with a truly random 63-bit value, if possible. More...
|
| |
| template<typename T > |
| void | set_seed (T &&seed) |
| | Sets the seed of the random number engine. More...
|
| |
| Engine::result_type | advance () |
| | Advance the engine's state and return the generated value. More...
|
| |
| template<typename T > |
| T | uniform (T min, T max) |
| |
| template<typename T > |
| T | uniform_int (T min, T max) |
| |
| template<typename T = double> |
| T | canonical () |
| |
| template<typename T = double> |
| T | canonical_nonzero () |
| |
| template<typename T > |
| uniform_dist< T > | make_uniform_distribution (T min, T max) |
| |
| template<typename T = double> |
| T | exponential (T lambda) |
| | Draws an exponentially distributed random number. More...
|
| |
| template<typename T = double> |
| T | expo (T A, T x1, T x2) |
| | Draws a random number x from an exponential distribution exp(A*x), where A is assumed to be positive, and x is typically negative. More...
|
| |
| template<typename T > |
| int | sgn (T val) |
| | Signum function. More...
|
| |
| template<typename T = double> |
| T | power (T n, T xMin, T xMax) |
| | Sample from a power-law probability density proportional to |x|^n. More...
|
| |
| template<typename T > |
| int | poisson (const T &lam) |
| | Returns a Poisson distributed random number. More...
|
| |
| template<typename T > |
| int | binomial (const int N, const T &p) |
| | Returns a binomially distributed random number. More...
|
| |
| template<typename T > |
| double | normal (const T &mean, const T &sigma) |
| | Returns a random number drawn from a normal distribution. More...
|
| |
| template<typename T = double> |
| T | cauchy (T pole, T width, T min, T max) |
| | Draws a random number from a Cauchy distribution (sometimes also called Lorentz or non-relativistic Breit-Wigner distribution) with the given parameters (constant width!) inside the range [min,max]. More...
|
| |
| template<typename T = double> |
| T | beta (T a, T b) |
| | Draws a random number from a beta-distribution, where probability density of \(x\) is \(p(x) = frac{\Gamma(a)\Gamma(b)}{Gamma(a+b)} x^{a-1} (1-x)^{b-1}\). More...
|
| |
| template<typename T = double> |
| T | beta_a0 (T xmin, T b) |
| | Draws a random number from a beta-distribution with a = 0. More...
|
| |
Namespace random provides functions for random Number Generation.
template<typename T = double>
| T smash::random::expo |
( |
T |
A, |
|
|
T |
x1, |
|
|
T |
x2 |
|
) |
| |
Draws a random number x from an exponential distribution exp(A*x), where A is assumed to be positive, and x is typically negative.
The result x is restricted to lie between x1 and x2 (with x2 < x <= x1).
- Parameters
-
| A | Positive shape parameter. |
| x1 | Maximal sampled value. |
| x2 | Minimal sampled value. |
- Returns
- Sampled random number.
- Exceptions
-
| std::logic_error | if the computed sampling interval is degenerate, reversed, or otherwise invalid. |
Definition at line 178 of file random.h.
179 const T a1 = A * x1, a2 = A * x2;
180 const T a_min = std::log(std::numeric_limits<T>::min());
181 assert(A > T(0.) && x1 > x2 && a1 > a_min);
182 const T high = std::exp(a1);
183 const T low = a2 > a_min ? std::exp(a2) : T(0.);
185 std::ostringstream error_message{};
186 error_message <<
"Function " << __func__
187 <<
": internal invariant 'low < high' violated (low = " << low
188 <<
", high = " << high <<
")";
189 throw std::logic_error(error_message.str());
195 x = std::log(
uniform(low, high)) / A;
196 }
while (!(x <= x1 && x > x2));
#define unlikely(x)
Tell the branch predictor that this expression is likely false.
template<typename T = double>
| T smash::random::power |
( |
T |
n, |
|
|
T |
xMin, |
|
|
T |
xMax |
|
) |
| |
Sample from a power-law probability density proportional to |x|^n.
The sample is drawn on the interval [xMin, xMax]. The interval must lie entirely on one side of zero; intervals crossing zero are not supported. Negative intervals are handled by sampling the absolute values and restoring the negative sign. For n ≈ -1, the distribution is sampled using the logarithmic limit.
- Template Parameters
-
- Parameters
-
| n | Power-law exponent. |
| xMin | Lower interval bound. |
| xMax | Upper interval bound. |
- Returns
- Random value distributed as p(x) ∝ |x|^n on the given interval.
- Exceptions
-
| std::invalid_argument | if the interval crosses zero. |
| std::invalid_argument | if the interval touches zero and n <= -1, where the distribution is not normalizable. |
Definition at line 229 of file random.h.
230 const T n1 =
n + T(1);
232 if ((xMin < 0 && xMax > 0) || (xMax < 0 && xMin > 0)) {
233 throw std::invalid_argument(
234 "power: interval crossing zero is not supported");
235 }
else if ((xMin == 0 || xMax == 0) &&
n <= -1) {
236 throw std::invalid_argument(
"power: distribution not normalizable at x=0");
240 std::swap(xMin, xMax);
242 const T sign = xMax < T(0) ? T(-1) : T(1);
244 const T lo = std::abs(xMin);
245 const T hi = std::abs(xMax);
247 if (std::abs(n1) < T(1e-3)) {
248 return sign * lo * std::pow(hi / lo,
canonical());
251 T a = std::pow(lo, n1);
252 T b = std::pow(hi, n1);
258 return sign * std::pow(
uniform(a, b), T(1) / n1);
template<typename T = double>
| T smash::random::cauchy |
( |
T |
pole, |
|
|
T |
width, |
|
|
T |
min, |
|
|
T |
max |
|
) |
| |
Draws a random number from a Cauchy distribution (sometimes also called Lorentz or non-relativistic Breit-Wigner distribution) with the given parameters (constant width!) inside the range [min,max].
This function is similar to std::cauchy_distribution, but can return values inside a limited interval.
- Parameters
-
| pole | Pole parameter of the Cauchy function, i.e. location of the peak. |
| width | Width parameter of the Cauchy function, determining the sharpness of the peak. |
| min | Minimum value to be returned. |
| max | Maximum value to be returned. |
- Returns
- Sampled random number.
Definition at line 351 of file random.h.
355 const double u_min = std::atan((min - pole) / width);
356 const double u_max = std::atan((max - pole) / width);
357 const double u =
uniform(u_min, u_max);
358 return pole + width * std::tan(u);
template<typename T = double>
| T smash::random::beta |
( |
T |
a, |
|
|
T |
b |
|
) |
| |
Draws a random number from a beta-distribution, where probability density of \(x\) is \(p(x) = frac{\Gamma(a)\Gamma(b)}{Gamma(a+b)} x^{a-1} (1-x)^{b-1}\).
This distribution is necessary for string formation. The implementation uses a property connecting beta distribution to gamma-distribution. Interchanging a and b will not change results.
- Parameters
-
| a | Shape parameter. |
| b | Scale parameter. |
- Returns
- Sampled random number.
Definition at line 373 of file random.h.
375 assert(a > T(0.0) && b > T(0.0));
376 const T x1 = std::gamma_distribution<T>(a)(
engine);
377 const T x2 = std::gamma_distribution<T>(b)(
engine);
378 return x1 / (x1 + x2);
template<typename T = double>
| T smash::random::beta_a0 |
( |
T |
xmin, |
|
|
T |
b |
|
) |
| |
Draws a random number from a beta-distribution with a = 0.
In this case the probability density is \(p(x) = 1/x (1-x)^b\). The integral from 0 to 1 over this distribution diverges, so the sampling is performed in the interval (xmin, 1). This distribution is necessary for string formation. The implementation uses the following property: \(p(x)dx = dx/x (1-x)^b = (1-x)^b d ln(x) = (1 - e^{-y})^b dy\), where \( y = - ln(x) \).
- Parameters
-
| xmin | Minimal sampled value. |
| b | Second shape parameter. |
- Returns
- Sampled random number.
Definition at line 395 of file random.h.
396 assert(xmin > T(0.0) && xmin < T(1.0));
399 y =
uniform(0.0, -std::log(xmin));
400 }
while (std::pow((1.0 - std::exp(-y)), b) <
canonical());