MaCh3  2.8.0
Reference Guide
Random.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <vector>
5 
6 #include "Manager/Core.h"
7 #include "Manager/Monitor.h"
8 
10 // ROOT include
11 #include "TRandom3.h"
13 
14 namespace M3::rand
15 {
23 class Random
24 {
25  public:
27  static Random& Instance()
28  {
29  static Random instance;
30  return instance;
31  }
32 
37  void SetSeed(const std::uint64_t seed)
38  {
39  fSeed = seed;
40  const auto nThreads = GetNThreads();
41  fEngines.clear();
42  fEngines.reserve(nThreads);
43 
44  for (int thread = 0; thread < nThreads; ++thread) {
45  fEngines.emplace_back(std::make_unique<TRandom3>(fSeed));
46  }
47  }
48 
50  std::uint64_t GetSeed() const {
51  return fSeed;
52  }
53 
55  TRandom3* Engine() {
56  return fEngines[ThreadID()].get();
57  }
58 
60  double Uniform(const double min = 0.0,
61  const double max = 1.0) {
62  return Engine()->Uniform(min, max);
63  }
64 
66  int UniformInt(const int min, const int max) {
67  return Engine()->Integer(max - min + 1) + min;
68  }
69 
71  double Gaus(const double mean = 0.0,
72  const double sigma = 1.0) {
73  return Engine()->Gaus(mean, sigma);
74  }
75 
77  double PoissonD(const double mean) {
78  return Engine()->PoissonD(mean);
79  }
80 
82  int Poisson(const double mean) {
83  #pragma GCC diagnostic push
84  #pragma GCC diagnostic ignored "-Wconversion"
85  return Engine()->Poisson(mean);
86  #pragma GCC diagnostic pop
87  }
88  private:
90  Random() {
91  SetSeed(0);
92  }
93 
95  static std::size_t ThreadID() {
96  #ifdef MULTITHREAD
97  return static_cast<std::size_t>(omp_get_thread_num());
98  #else
99  return 0;
100  #endif
101  }
102 
104  std::uint64_t fSeed = 0;
105 
107  std::vector<std::unique_ptr<TRandom3>> fEngines;
108 };
109 
111  inline void SetSeed(const std::uint64_t seed) {
112  Random::Instance().SetSeed(seed);
113  }
114 
116  inline double Uniform(const double min = 0.0,
117  const double max = 1.0) {
118  return Random::Instance().Uniform(min, max);
119  }
120 
122  inline int Poisson(const double mean) {
123  return Random::Instance().Poisson(mean);
124  }
125 
127  inline double PoissonD(const double mean) {
128  return Random::Instance().PoissonD(mean);
129  }
130 
132  inline int UniformInt(const int min, const int max) {
133  return Random::Instance().UniformInt(min, max);
134  }
135 
137  inline double Gaus(const double mean = 0.0,
138  const double sigma = 1.0) {
139  return Random::Instance().Gaus(mean, sigma);
140  }
141 } // namespace M3
KS: Core MaCh3 definitions and compile-time configuration utilities.
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
Definition: Core.h:126
#define _MaCh3_Safe_Include_End_
System and monitoring utilities for printing system information and status updates.
Universal random number generator for MaCh3.
Definition: Random.h:24
static Random & Instance()
Get the global Random instance.
Definition: Random.h:27
Random()
Constructor.
Definition: Random.h:90
void SetSeed(const std::uint64_t seed)
Set the global seed and initialise all RNG streams.
Definition: Random.h:37
double Uniform(const double min=0.0, const double max=1.0)
Generate a uniform random number in [min, max).
Definition: Random.h:60
std::uint64_t GetSeed() const
Get the global seed.
Definition: Random.h:50
double Gaus(const double mean=0.0, const double sigma=1.0)
Generate a Gaussian random number.
Definition: Random.h:71
TRandom3 * Engine()
Get the random generator associated with the current thread.
Definition: Random.h:55
int Poisson(const double mean)
Generate an integer Poisson-distributed random number with the given mean.
Definition: Random.h:82
static std::size_t ThreadID()
Get the current OpenMP thread ID.
Definition: Random.h:95
int UniformInt(const int min, const int max)
Generate a uniform random integer in [min, max].
Definition: Random.h:66
std::uint64_t fSeed
The global seed.
Definition: Random.h:104
double PoissonD(const double mean)
Generate a double-precision Poisson-distributed random number with the given mean.
Definition: Random.h:77
std::vector< std::unique_ptr< TRandom3 > > fEngines
One random generator per thread.
Definition: Random.h:107
void SetSeed(const std::uint64_t seed)
Set the global random seed.
Definition: Random.h:111
double PoissonD(const double mean)
Generate a Poisson-distributed random number with the given mean.
Definition: Random.h:127
double Uniform(const double min=0.0, const double max=1.0)
Generate a uniform random number in [min, max).
Definition: Random.h:116
int Poisson(const double mean)
Generate a Poisson-distributed integer with the given mean.
Definition: Random.h:122
double Gaus(const double mean=0.0, const double sigma=1.0)
Generate a Gaussian random number.
Definition: Random.h:137
int UniformInt(const int min, const int max)
Generate a uniform random integer in [min, max].
Definition: Random.h:132
int GetNThreads()
number of threads which we need for example for TRandom3
Definition: Monitor.cpp:372