Garfield++ 4.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
Random.hh
Go to the documentation of this file.
1#ifndef G_RANDOM_H
2#define G_RANDOM_H
3
4#include <cmath>
6#include "RandomEngineRoot.hh"
7
8namespace Garfield {
9
10/// Random number generator
11extern RandomEngineRoot randomEngine;
12
13/// Draw a random number uniformly distributed in the range [0, 1).
14inline double RndmUniform() { return randomEngine.Draw(); }
15
16/// Draw a random number uniformly distributed in the range (0, 1).
17inline double RndmUniformPos() {
18 double r = RndmUniform();
19 while (r <= 0.) r = RndmUniform();
20 return r;
21}
22
23/// Draw a Gaussian random variate with mean zero and standard deviation one.
24inline double RndmGaussian() {
25 static bool cached = false;
26 static double u = 0.;
27 if (cached) {
28 cached = false;
29 return u;
30 }
31 // Box-Muller algorithm
32 u = 2. * RndmUniform() - 1.;
33 double v = 2. * RndmUniform() - 1.;
34 double r2 = u * u + v * v;
35 while (r2 > 1.) {
36 u = 2. * RndmUniform() - 1.;
37 v = 2. * RndmUniform() - 1.;
38 r2 = u * u + v * v;
39 }
40 const double p = sqrt(-2. * log(r2) / r2);
41 u *= p;
42 cached = true;
43 return v * p;
44}
45
46/// Draw a Gaussian random variate with mean mu and standard deviation sigma.
47inline double RndmGaussian(const double mu, const double sigma) {
48 return mu + sigma * RndmGaussian();
49}
50
51/// Draw a Lorentzian random variate with mean mu
52/// and half-width at half maximum gamma.
53inline double RndmLorentzian(const double mu, const double gamma) {
54 return mu + gamma * tan(Pi * (RndmUniform() - 0.5));
55}
56
57/// Draw a random number according to a Voigt function with mean mu.
58/// The Voigt function is a convolution of a
59/// Gaussian (standard deviation sigma) and
60/// a Lorentzian (half width gamma).
61inline double RndmVoigt(const double mu, const double sigma,
62 const double gamma) {
63 if (sigma <= 0.) return RndmLorentzian(mu, gamma);
64 const double a = gamma / (Sqrt2 * sigma);
65 const double x = RndmLorentzian(0., a) + RndmGaussian(0., 1. / Sqrt2);
66 return mu + x * Sqrt2 * sigma;
67}
68
69/// Draw a Polya distributed random number.
70inline double RndmPolya(const double theta) {
71 // Algorithm from Review of Particle Physics
72 // C. Amsler et al, Phys. Lett. B 667 (2008)
73 if (theta <= 0.) return -log(RndmUniformPos());
74 const double c = 3 * (theta + 1.) - 0.75;
75 double u1, u2, v1, v2, v3;
76 double x;
77 while (1) {
78 u1 = RndmUniformPos();
79 v1 = u1 * (1. - u1);
80 if (v1 == 0.) continue;
81 v2 = (u1 - 0.5) * sqrt(c / v1);
82 x = theta + v2;
83 if (x <= 0.) continue;
84 u2 = RndmUniformPos();
85 v3 = 64 * u2 * u2 * pow(v1, 3);
86 if (v3 <= 1. - 2 * v2 * v2 / x ||
87 log(v3) <= 2 * (theta * log(x / theta) - v2)) {
88 return x / (theta + 1.);
89 }
90 }
91}
92
93/// Draw a random number from a Landau distribution.
94double RndmLandau();
95/// Draw a random number from a Vavilov distribution.
96double RndmVavilov(const double rkappa, const double beta2);
97
98/// Draw a random number from a Poisson distribution.
99int RndmPoisson(const double mean);
100
101/// Draw a random energy needed to create a single electron in
102/// a material asymptotic work function W and Fano factor F,
103/// according to Igor Smirnov's phenomenological model.
104double RndmHeedWF(const double w, const double f);
105
106/// Draw a random (isotropic) direction vector.
107inline void RndmDirection(double& dx, double& dy, double& dz,
108 const double length = 1.) {
109 const double phi = TwoPi * RndmUniform();
110 const double ctheta = 2 * RndmUniform() - 1.;
111 const double stheta = sqrt(1. - ctheta * ctheta);
112 dx = length * cos(phi) * stheta;
113 dy = length * sin(phi) * stheta;
114 dz = length * ctheta;
115}
116}
117
118#endif
double Draw() override
Call the random number generator.
double RndmLorentzian(const double mu, const double gamma)
Definition: Random.hh:53
double RndmHeedWF(const double w, const double f)
Definition: Random.cc:699
double RndmUniform()
Draw a random number uniformly distributed in the range [0, 1).
Definition: Random.hh:14
double RndmVavilov(const double rkappa, const double beta2)
Draw a random number from a Vavilov distribution.
Definition: Random.cc:300
double RndmPolya(const double theta)
Draw a Polya distributed random number.
Definition: Random.hh:70
void RndmDirection(double &dx, double &dy, double &dz, const double length=1.)
Draw a random (isotropic) direction vector.
Definition: Random.hh:107
double RndmVoigt(const double mu, const double sigma, const double gamma)
Definition: Random.hh:61
double RndmUniformPos()
Draw a random number uniformly distributed in the range (0, 1).
Definition: Random.hh:17
double RndmGaussian()
Draw a Gaussian random variate with mean zero and standard deviation one.
Definition: Random.hh:24
double RndmLandau()
Draw a random number from a Landau distribution.
Definition: Random.cc:104
RandomEngineRoot randomEngine
Random number generator.
int RndmPoisson(const double mean)
Draw a random number from a Poisson distribution.
Definition: Random.cc:664