Version: SMASH-3.1
smash::random Namespace Reference

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

Classes

class  uniform_dist
 Provides uniform random numbers on a fixed interval. More...
 
class  discrete_dist
 Discrete distribution with weight given by probability vector. More...
 
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...
 

Typedefs

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

Functions

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 >
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

◆ generate_63bit_seed()

int64_t smash::random::generate_63bit_seed ( )

Generates a seed with a truly random 63-bit value, if possible.

Definition at line 20 of file random.cc.

20  {
21  std::random_device rd;
22  static_assert(std::is_same<decltype(rd()), uint32_t>::value,
23  "random_device is assumed to generate uint32_t");
24  uint64_t unsigned_seed =
25  (static_cast<uint64_t>(rd()) << 32) | static_cast<uint64_t>(rd());
26  // Discard the highest bit to make sure it fits into a positive int64_t
27  const int64_t seed = static_cast<int64_t>(unsigned_seed >> 1);
28  return seed;
29 }

◆ set_seed()

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

Sets the seed of the random number engine.

Definition at line 71 of file random.h.

71  {
72  static_assert(std::is_same<Engine::result_type, uint64_t>::value,
73  "experiment.cc needs the seed to be 64 bits");
74  engine.seed(std::forward<T>(seed));
75 }
Engine engine
The engine that is used commonly by all distributions.
Definition: random.cc:18

◆ advance()

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

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

Definition at line 78 of file random.h.

78 { return engine(); }

◆ 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 88 of file random.h.

88  {
89  return std::uniform_real_distribution<T>(min, max)(engine);
90 }

◆ 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 100 of file random.h.

100  {
101  return std::uniform_int_distribution<T>(min, max)(engine);
102 }

◆ 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 113 of file random.h.

113  {
114  return std::generate_canonical<T, std::numeric_limits<double>::digits>(
115  engine);
116 }

◆ canonical_nonzero()

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

Definition at line 122 of file random.h.

122  {
123  // use 'nextafter' to generate a value that is guaranteed to be larger than 0
124  return std::nextafter(
125  std::generate_canonical<T, std::numeric_limits<double>::digits>(engine),
126  T(1));
127 }

◆ 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 135 of file random.h.

135  {
136  return uniform_dist<T>(min, max);
137 }

◆ 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 149 of file random.h.

149  {
150  // We are not using std::exponential_distribution because of a bug in the
151  // implementations by clang and gcc.
152  return -std::log(canonical_nonzero()) / lambda;
153 }
T canonical_nonzero()
Definition: random.h:122

◆ 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 166 of file random.h.

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

◆ 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 190 of file random.h.

190  {
191  return (T(0) < val) - (val < T(0));
192 }

◆ 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 203 of file random.h.

203  {
204  const T n1 = n + 1;
205  if (std::abs(n1) < 1E-3) {
206  return xMin * std::pow(xMax / xMin, canonical());
207  } else if (xMin >= 0. && xMax > 0.) {
208  return std::pow(uniform(std::pow(xMin, n1), std::pow(xMax, n1)), 1. / n1);
209  } else {
210  return sgn(xMin) * std::pow(uniform(std::pow(std::abs(xMin), n1),
211  std::pow(std::abs(xMax), n1)),
212  1. / n1);
213  }
214 }
constexpr int n
Neutron.
int sgn(T val)
Signum function.
Definition: random.h:190
T canonical()
Definition: random.h:113

◆ 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 226 of file random.h.

226  {
227  return std::poisson_distribution<int>(lam)(engine);
228 }

◆ 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 238 of file random.h.

238  {
239  return std::binomial_distribution<int>(N, p)(engine);
240 }
constexpr int p
Proton.

◆ 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 250 of file random.h.

250  {
251  return std::normal_distribution<double>(mean, sigma)(engine);
252 }

◆ 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 307 of file random.h.

307  {
308  /* Use double-precision variables, in order to work around a glibc bug in
309  * tanf:
310  * https://sourceware.org/bugzilla/show_bug.cgi?id=18221 */
311  const double u_min = std::atan((min - pole) / width);
312  const double u_max = std::atan((max - pole) / width);
313  const double u = uniform(u_min, u_max);
314  return pole + width * std::tan(u);
315 }

◆ 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 329 of file random.h.

329  {
330  // Otherwise the integral over probability density diverges
331  assert(a > T(0.0) && b > T(0.0));
332  const T x1 = std::gamma_distribution<T>(a)(engine);
333  const T x2 = std::gamma_distribution<T>(b)(engine);
334  return x1 / (x1 + x2);
335 }

◆ 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 351 of file random.h.

351  {
352  assert(xmin > T(0.0) && xmin < T(1.0));
353  T y;
354  do {
355  y = uniform(0.0, -std::log(xmin));
356  } while (std::pow((1.0 - std::exp(-y)), b) < canonical());
357  return std::exp(-y);
358 }

Variable Documentation

◆ engine

random::Engine smash::random::engine
extern

The engine that is used commonly by all distributions.

Definition at line 18 of file random.cc.