ATLAS Offline Software
random.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 /*
3  * Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration.
4  */
19 #ifndef TESTTOOLS_RANDOM_H
20 #define TESTTOOLS_RANDOM_H
21 
22 
23 // Can't use CxxUtils/checker_macros.h here, since that would be circular dependency.
24 #ifdef ATLAS_GCC_CHECKERS
25 #define ATLAS_NOT_REENTRANT [[ATLAS::not_reentrant]]
26 #else
27 #define ATLAS_NOT_REENTRANT
28 #endif
29 
30 
31 #include <stdint.h>
32 #include <cmath>
33 
34 
35 namespace Athena_test {
36 
37 
39 static const uint32_t rngmax = static_cast<uint32_t> (-1);
40 
41 
43 inline
45 {
46  seed = (1664525*seed + 1013904223);
47  return seed;
48 }
49 
50 
52 inline
53 float randf_seed (uint32_t& seed, float rmax, float rmin = 0)
54 {
55  return static_cast<float>(rng_seed(seed)) / static_cast<float>(rngmax) * (rmax-rmin) + rmin;
56 }
57 
58 
60 inline
61 int randi_seed (uint32_t& seed, int rmax, int rmin = 0)
62 {
63  return static_cast<int> (randf_seed (seed, rmax, rmin));
64 }
65 
66 
68 struct RNG
69 {
70  RNG() : seed(1) {}
71  int operator() (int n) { return randi_seed (seed, n); }
73 };
74 
75 
77 struct URNG
78 {
80  URNG(uint32_t the_seed = 1) : seed(the_seed) {}
81  static constexpr result_type min() { return 0; }
82  static constexpr result_type max() { return 1000000; }
85 };
86 
87 
88 static uint32_t seed = 1;
89 inline
91 inline
92 int randi ATLAS_NOT_REENTRANT (int rmax, int rmin = 0) { return randi_seed (seed, rmax, rmin); }
93 inline
94 float randf ATLAS_NOT_REENTRANT (float rmax, float rmin = 0) { return randf_seed (seed, rmax, rmin); }
95 
96 
97 // Distribution objects like in <random>.
98 // Reimplemented here so that we can be sure of getting reproducible results.
99 
100 template <class T>
102 {
103 public:
104  normal_distribution (T mu, T s) : m_mu(mu), m_s(s) {}
105  T operator() (URNG& rng) const;
106 
107 private:
108  T m_mu;
109  T m_s;
110 };
111 
112 
113 template <class T>
115 {
116  // Stamdard Box-Muller transform a la Numerical Recipes, except that for simplicity
117  // i don't try to remember the second generated number between calls. I don't
118  // really care about performance here.
119  T v1, v2, r2;
120  do {
121  v1 = 2*(static_cast<T>(rng()) / URNG::max())-1;
122  v2 = 2*(static_cast<T>(rng()) / URNG::max())-1;
123  r2 = v1*v1 + v2*v2;
124  } while (r2 >= 1);
125 
126  T fac = sqrt(-2*log(r2) / r2);
127  return v1*fac*m_s + m_mu;
128 }
129 
130 
131 template <class T>
133 {
134 public:
136  T operator() (URNG& rng) const
137  {
138  return static_cast<T>(rng())/URNG::max()*(m_b-m_a) + m_a;
139  }
140 
141 private:
142  T m_a;
143  T m_b;
144 };
145 
146 
147 } // namespace Athena_test
148 
149 
150 #endif // not TESTTOOLS_RANDOM_H
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
Athena_test::uniform_real_distribution::m_b
T m_b
Definition: random.h:143
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
Athena_test::randi_seed
int randi_seed(uint32_t &seed, int rmax, int rmin=0)
Generate an integer random number between rmin and rmax.
Definition: random.h:61
Athena_test::normal_distribution::normal_distribution
normal_distribution(T mu, T s)
Definition: random.h:104
Athena_test::URNG::URNG
URNG(uint32_t the_seed=1)
Definition: random.h:80
Athena_test::normal_distribution::operator()
T operator()(URNG &rng) const
Definition: random.h:114
Athena_test::URNG::operator()
result_type operator()()
Definition: random.h:83
Athena_test::RNG::RNG
RNG()
Definition: random.h:70
D3PDTest::rng
uint32_t rng()
Definition: FillerAlg.cxx:40
Athena_test::normal_distribution
Definition: random.h:102
Athena_test::uniform_real_distribution::uniform_real_distribution
uniform_real_distribution(T a, T b)
Definition: random.h:135
Athena_test::URNG::result_type
uint32_t result_type
Definition: random.h:79
beamspotman.n
n
Definition: beamspotman.py:731
Athena_test::uniform_real_distribution::operator()
T operator()(URNG &rng) const
Definition: random.h:136
Athena_test::URNG::max
static constexpr result_type max()
Definition: random.h:82
D3PDTest::randf
double randf(double rmax, double rmin=0)
Definition: FillerAlg.cxx:46
Athena_test
functions & macros to test the difference between floats
Definition: InitGaudiGoogleTest.h:30
Athena_test::normal_distribution::m_mu
T m_mu
Definition: random.h:108
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
Athena_test::uniform_real_distribution::m_a
T m_a
Definition: random.h:142
Athena_test::URNG::min
static constexpr result_type min()
Definition: random.h:81
Athena_test::RNG::seed
uint32_t seed
Definition: random.h:72
ReadCellNoiseFromCoolCompare.v2
v2
Definition: ReadCellNoiseFromCoolCompare.py:364
Athena_test::RNG::operator()
int operator()(int n)
Definition: random.h:71
a
TList * a
Definition: liststreamerinfos.cxx:10
Athena_test::normal_distribution::m_s
T m_s
Definition: random.h:109
Athena_test::ATLAS_NOT_REENTRANT
uint32_t rng ATLAS_NOT_REENTRANT()
Definition: random.h:90
Athena_test::URNG
Generator compatible with the C++11 STL UniformRandomNumberGenerator.
Definition: random.h:78
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
Athena_test::uniform_real_distribution
Definition: random.h:133
Athena_test::RNG
Generator compatible with the STL RandomNumberGenerator.
Definition: random.h:69
Athena_test::rng_seed
uint32_t rng_seed(uint32_t &seed)
Generate a random number between 0 and rngmax.
Definition: random.h:44
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
Athena_test::URNG::seed
uint32_t seed
Definition: random.h:84
Athena_test::randf_seed
float randf_seed(uint32_t &seed, float rmax, float rmin=0)
Generate a floating-point random number between rmin and rmax.
Definition: random.h:53