Version: SMASH-1.5
smash::random Namespace Reference

Namespace random provides functions for random Number Generation. More...

Classes

class  BesselSampler
 The intention of this class is to efficiently sample \( (N_1, N_2) \) from the Bessel distribution \( p(N_1,N_2) \sim \mathrm{Poi}(\nu_1) \mathrm{Poi}(\nu_2) \delta(N_1 - N_2 = N)\), where \(\mathrm{Poi}(\nu)\) denotes Poisson distribution with mean \(\nu\). More...
 
class  discrete_dist
 Discrete distribution with weight given by probability vector. More...
 
class  uniform_dist
 Provides uniform random numbers on a fixed interval. More...
 

Typedefs

using Engine = std::mt19937_64
 The random number engine used is the Mersenne Twister. More...
 

Functions

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 >
uniform (T min, T max)
 
template<typename T >
uniform_int (T min, T max)
 
template<typename T = double>
canonical ()
 
template<typename T = double>
canonical_nonzero ()
 
template<typename T >
uniform_dist< T > make_uniform_distribution (T min, T max)
 
template<typename T = double>
exponential (T lambda)
 Draws an exponentially distributed random number. More...
 
template<typename T = double>
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>
power (T n, T xMin, T xMax)
 Draws a random number according to a power-law distribution ~ 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>
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>
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>
beta_a0 (T xmin, T b)
 Draws a random number from a beta-distribution with a = 0. More...
 

Variables

Engine engine
 The engine that is used commonly by all distributions. More...
 

Detailed Description

Namespace random provides functions for random Number Generation.

Typedef Documentation

◆ Engine

using smash::random::Engine = typedef std::mt19937_64

The random number engine used is the Mersenne Twister.

Definition at line 27 of file random.h.

Function Documentation

◆ set_seed()

template<typename T >
void smash::random::set_seed ( T &&  seed)

Sets the seed of the random number engine.

Definition at line 68 of file random.h.

68  {
69  static_assert(std::is_same<Engine::result_type, uint64_t>::value,
70  "experiment.cc needs the seed to be 64 bits");
71  engine.seed(std::forward<T>(seed));
72 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ advance()

Engine::result_type smash::random::advance ( )
inline

Advance the engine's state and return the generated value.

Definition at line 75 of file random.h.

75 { return engine(); }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ uniform()

template<typename T >
T smash::random::uniform ( min,
max 
)
Returns
A uniformly distributed random real number \(\chi \in [{\rm min}, {\rm max})\)
Parameters
minMinimal sampled value.
maxMaximal sampled value.

Definition at line 85 of file random.h.

85  {
86  return std::uniform_real_distribution<T>(min, max)(engine);
87 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ uniform_int()

template<typename T >
T smash::random::uniform_int ( min,
max 
)
Returns
A uniformly distributed random integer number \(\chi \in [{\rm min}, {\rm max})\)
Parameters
minMinimal sampled value.
maxMaximal sampled value.

Definition at line 97 of file random.h.

97  {
98  return std::uniform_int_distribution<T>(min, max)(engine);
99 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ canonical()

template<typename T = double>
T smash::random::canonical ( )
Returns
a uniformly distributed random number \(\chi \in [0,1)\).

Note that the popular implementations in GCC and clang may return 1:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64351 https://llvm.org/bugs/show_bug.cgi?id=18767

Definition at line 110 of file random.h.

110  {
111  return std::generate_canonical<T, std::numeric_limits<double>::digits>(
112  engine);
113 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ canonical_nonzero()

template<typename T = double>
T smash::random::canonical_nonzero ( )
Returns
A uniformly distributed random number \(\chi \in (0,1]\).

Definition at line 119 of file random.h.

119  {
120  // use 'nextafter' to generate a value that is guaranteed to be larger than 0
121  return std::nextafter(
122  std::generate_canonical<T, std::numeric_limits<double>::digits>(engine),
123  T(1));
124 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ make_uniform_distribution()

template<typename T >
uniform_dist<T> smash::random::make_uniform_distribution ( min,
max 
)
Returns
A uniform_dist object.
Parameters
minLower bound of interval.
maxUpper bound of interval.

Definition at line 132 of file random.h.

132  {
133  return uniform_dist<T>(min, max);
134 }
Here is the caller graph for this function:

◆ exponential()

template<typename T = double>
T smash::random::exponential ( lambda)

Draws an exponentially distributed random number.

Probability for a given return value \(\chi\) is \(p(\chi) = \Theta(\chi) \cdot \exp(-t)\).

Parameters
lambdaRate parameter.
Returns
Sampled random number.

Definition at line 146 of file random.h.

146  {
147  // We are not using std::exponential_distribution because of a bug in the
148  // implementations by clang and gcc.
149  return -std::log(canonical_nonzero()) / lambda;
150 }
T canonical_nonzero()
Definition: random.h:119
Here is the call graph for this function:

◆ expo()

template<typename T = double>
T smash::random::expo ( A,
x1,
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
APositive shape parameter.
x1Maximal sampled value.
x2Minimal sampled value.
Returns
Sampled random number.

Definition at line 163 of file random.h.

163  {
164  const T a1 = A * x1, a2 = A * x2;
165  const T a_min = std::log(std::numeric_limits<T>::min());
166 #ifndef NDEBUG
167  assert(A > T(0.) && x1 >= x2 && a1 > a_min);
168 #endif
169  const T r1 = std::exp(a1);
170  const T r2 = a2 > a_min ? std::exp(a2) : T(0.); // prevent underflow
171  T x;
172  do {
173  /* sample repeatedly until x is in the requested range
174  * (it can get outside due to numerical errors, see issue #2959) */
175  x = std::log(uniform(r1, r2)) / A;
176  } while (!(x <= x1 && x > x2));
177  return x;
178 }
T uniform(T min, T max)
Definition: random.h:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sgn()

template<typename T >
int smash::random::sgn ( val)

Signum function.

Parameters
valThe input value.
Returns
The sign of the input value.

Definition at line 187 of file random.h.

187  {
188  return (T(0) < val) - (val < T(0));
189 }
Here is the caller graph for this function:

◆ power()

template<typename T = double>
T smash::random::power ( n,
xMin,
xMax 
)

Draws a random number according to a power-law distribution ~ x^n.

Parameters
nExponent in power law (arbitrary real number).
xMinMinimum value.
xMaxMaximum value.
Returns
random number between xMin and xMax.

Definition at line 200 of file random.h.

200  {
201  const T n1 = n + 1;
202  if (std::abs(n1) < 1E-3) {
203  return xMin * std::pow(xMax / xMin, canonical());
204  } else if (xMin > 0. && xMax > 0.) {
205  return std::pow(uniform(std::pow(xMin, n1), std::pow(xMax, n1)), 1. / n1);
206  } else {
207  return sgn(xMin) * std::pow(uniform(std::pow(std::abs(xMin), n1),
208  std::pow(std::abs(xMax), n1)),
209  1. / n1);
210  }
211 }
T canonical()
Definition: random.h:110
T uniform(T min, T max)
Definition: random.h:85
constexpr int n
Neutron.
int sgn(T val)
Signum function.
Definition: random.h:187
Here is the call graph for this function:
Here is the caller graph for this function:

◆ poisson()

template<typename T >
int smash::random::poisson ( const T &  lam)

Returns a Poisson distributed random number.

Probability for a given return value \(\chi\) is \(p(\chi) = \chi^i/i! \cdot \exp(-\chi)\)

Parameters
lamMean value of the distribution.
Returns
Sampled random number.

Definition at line 223 of file random.h.

223  {
224  return std::poisson_distribution<int>(lam)(engine);
225 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ binomial()

template<typename T >
int smash::random::binomial ( const int  N,
const T &  p 
)

Returns a binomially distributed random number.

Parameters
NNumber of trials.
pProbability of a trial generating true.
Returns
Sampled random number.

Definition at line 235 of file random.h.

235  {
236  return std::binomial_distribution<int>(N, p)(engine);
237 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
constexpr int p
Proton.
Here is the caller graph for this function:

◆ normal()

template<typename T >
double smash::random::normal ( const T &  mean,
const T &  sigma 
)

Returns a random number drawn from a normal distribution.

Parameters
meanMean value of the distribution.
sigmaStandard deviation of the distribution.
Returns
Sampled random number.

Definition at line 247 of file random.h.

247  {
248  return std::normal_distribution<double>(mean, sigma)(engine);
249 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ cauchy()

template<typename T = double>
T smash::random::cauchy ( pole,
width,
min,
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
polePole parameter of the Cauchy function, i.e. location of the peak.
widthWidth parameter of the Cauchy function, determining the sharpness of the peak.
minMinimum value to be returned.
maxMaximum value to be returned.
Returns
Sampled random number.

Definition at line 304 of file random.h.

304  {
305  /* Use double-precision variables, in order to work around a glibc bug in
306  * tanf:
307  * https://sourceware.org/bugzilla/show_bug.cgi?id=18221 */
308  const double u_min = std::atan((min - pole) / width);
309  const double u_max = std::atan((max - pole) / width);
310  const double u = uniform(u_min, u_max);
311  return pole + width * std::tan(u);
312 }
T uniform(T min, T max)
Definition: random.h:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ beta()

template<typename T = double>
T smash::random::beta ( a,
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
aShape parameter.
bScale parameter.
Returns
Sampled random number.

Definition at line 326 of file random.h.

326  {
327  // Otherwise the integral over probability density diverges
328  assert(a > T(0.0) && b > T(0.0));
329  const T x1 = std::gamma_distribution<T>(a)(engine);
330  const T x2 = std::gamma_distribution<T>(b)(engine);
331  return x1 / (x1 + x2);
332 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:15
Here is the caller graph for this function:

◆ beta_a0()

template<typename T = double>
T smash::random::beta_a0 ( xmin,
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
xminMinimal sampled value.
bSecond shape parameter.
Returns
Sampled random number.

Definition at line 348 of file random.h.

348  {
349  assert(xmin > T(0.0) && xmin < T(1.0));
350  T y;
351  do {
352  y = uniform(0.0, -std::log(xmin));
353  } while (std::pow((1.0 - std::exp(-y)), b) < canonical());
354  return std::exp(-y);
355 }
T canonical()
Definition: random.h:110
T uniform(T min, T max)
Definition: random.h:85
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ engine

random::Engine smash::random::engine

The engine that is used commonly by all distributions.

Definition at line 15 of file random.cc.