ATLAS Offline Software
Loading...
Searching...
No Matches
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 */
17
18
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
35namespace Athena_test {
36
37
39static const uint32_t rngmax = static_cast<uint32_t> (-1);
40
41
43inline
44uint32_t rng_seed (uint32_t& seed)
45{
46 seed = (1664525*seed + 1013904223);
47 return seed;
48}
49
50
52inline
53float 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
60inline
61int randi_seed (uint32_t& seed, int rmax, int rmin = 0)
62{
63 return static_cast<int> (randf_seed (seed, rmax, rmin));
64}
65
66
68struct RNG
69{
70 RNG() : seed(1) {}
71 int operator() (int n) { return randi_seed (seed, n); }
72 uint32_t seed;
73};
74
75
77struct URNG
78{
79 typedef uint32_t result_type;
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; }
84 uint32_t seed;
85};
86
87
88static uint32_t seed = 1;
89inline
90uint32_t rng ATLAS_NOT_REENTRANT () { return rng_seed(seed); }
91inline
92int randi ATLAS_NOT_REENTRANT (int rmax, int rmin = 0) { return randi_seed (seed, rmax, rmin); }
93inline
94float 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
100template <class T>
102{
103public:
104 normal_distribution (T mu, T s) : m_mu(mu), m_s(s) {}
105 T operator() (URNG& rng) const;
106
107private:
110};
111
112
113template <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
131template <class T>
133{
134public:
136 T operator() (URNG& rng) const
137 {
138 return static_cast<T>(rng())/URNG::max()*(m_b-m_a) + m_a;
139 }
140
141private:
144};
145
146
147} // namespace Athena_test
148
149
150#endif // not TESTTOOLS_RANDOM_H
static Double_t a
T operator()(URNG &rng) const
Definition random.h:114
functions & macros to test the difference between floats
uint32_t rng ATLAS_NOT_REENTRANT()
Definition random.h:90
static const uint32_t rngmax
Maximum number generated.
Definition random.h:39
static uint32_t seed
Definition random.h:88
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
uint32_t rng_seed(uint32_t &seed)
Generate a random number between 0 and rngmax.
Definition random.h:44
int randi_seed(uint32_t &seed, int rmax, int rmin=0)
Generate an integer random number between rmin and rmax.
Definition random.h:61
uint32_t seed
Definition random.h:72
int operator()(int n)
Definition random.h:71
Generator compatible with the C++11 STL UniformRandomNumberGenerator.
Definition random.h:78
static constexpr result_type min()
Definition random.h:81
result_type operator()()
Definition random.h:83
URNG(uint32_t the_seed=1)
Definition random.h:80
static constexpr result_type max()
Definition random.h:82
uint32_t seed
Definition random.h:84
uint32_t result_type
Definition random.h:79