MaCh3  2.8.0
Reference Guide
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
M3::rand::Random Class Reference

Universal random number generator for MaCh3. More...

#include <Manager/Random.h>

Collaboration diagram for M3::rand::Random:
[legend]

Public Member Functions

void SetSeed (const std::uint64_t seed)
 Set the global seed and initialise all RNG streams. More...
 
std::uint64_t GetSeed () const
 Get the global seed. More...
 
TRandom3 * Engine ()
 Get the random generator associated with the current thread. More...
 
double Uniform (const double min=0.0, const double max=1.0)
 Generate a uniform random number in [min, max). More...
 
int UniformInt (const int min, const int max)
 Generate a uniform random integer in [min, max]. More...
 
double Gaus (const double mean=0.0, const double sigma=1.0)
 Generate a Gaussian random number. More...
 
double PoissonD (const double mean)
 Generate a double-precision Poisson-distributed random number with the given mean. More...
 
int Poisson (const double mean)
 Generate an integer Poisson-distributed random number with the given mean. More...
 

Static Public Member Functions

static Random & Instance ()
 Get the global Random instance. More...
 

Private Member Functions

 Random ()
 Constructor. More...
 

Static Private Member Functions

static std::size_t ThreadID ()
 Get the current OpenMP thread ID. More...
 

Private Attributes

std::uint64_t fSeed = 0
 The global seed. More...
 
std::vector< std::unique_ptr< TRandom3 > > fEngines
 One random generator per thread. More...
 

Detailed Description

Universal random number generator for MaCh3.

Note
STL random-number generators were also benchmarked, but ROOT's TRandom3 was faster for the tested workloads. The implementation can be switched to an STL engine if needed.
Author
Kamil Skwarczynski

Definition at line 23 of file Random.h.

Constructor & Destructor Documentation

◆ Random()

M3::rand::Random::Random ( )
inlineprivate

Constructor.

Definition at line 90 of file Random.h.

90  {
91  SetSeed(0);
92  }
void SetSeed(const std::uint64_t seed)
Set the global seed and initialise all RNG streams.
Definition: Random.h:37

Member Function Documentation

◆ Engine()

TRandom3* M3::rand::Random::Engine ( )
inline

Get the random generator associated with the current thread.

Definition at line 55 of file Random.h.

55  {
56  return fEngines[ThreadID()].get();
57  }
static std::size_t ThreadID()
Get the current OpenMP thread ID.
Definition: Random.h:95
std::vector< std::unique_ptr< TRandom3 > > fEngines
One random generator per thread.
Definition: Random.h:107

◆ Gaus()

double M3::rand::Random::Gaus ( const double  mean = 0.0,
const double  sigma = 1.0 
)
inline

Generate a Gaussian random number.

Definition at line 71 of file Random.h.

72  {
73  return Engine()->Gaus(mean, sigma);
74  }
TRandom3 * Engine()
Get the random generator associated with the current thread.
Definition: Random.h:55

◆ GetSeed()

std::uint64_t M3::rand::Random::GetSeed ( ) const
inline

Get the global seed.

Definition at line 50 of file Random.h.

50  {
51  return fSeed;
52  }
std::uint64_t fSeed
The global seed.
Definition: Random.h:104

◆ Instance()

static Random& M3::rand::Random::Instance ( )
inlinestatic

Get the global Random instance.

Definition at line 27 of file Random.h.

28  {
29  static Random instance;
30  return instance;
31  }
Random()
Constructor.
Definition: Random.h:90

◆ Poisson()

int M3::rand::Random::Poisson ( const double  mean)
inline

Generate an integer Poisson-distributed random number with the given mean.

Definition at line 82 of file Random.h.

82  {
83  #pragma GCC diagnostic push
84  #pragma GCC diagnostic ignored "-Wconversion"
85  return Engine()->Poisson(mean);
86  #pragma GCC diagnostic pop
87  }

◆ PoissonD()

double M3::rand::Random::PoissonD ( const double  mean)
inline

Generate a double-precision Poisson-distributed random number with the given mean.

Definition at line 77 of file Random.h.

77  {
78  return Engine()->PoissonD(mean);
79  }

◆ SetSeed()

void M3::rand::Random::SetSeed ( const std::uint64_t  seed)
inline

Set the global seed and initialise all RNG streams.

This should be called once at the beginning of the executable, before any multithreaded work is started.

Definition at line 37 of file Random.h.

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  }
int GetNThreads()
number of threads which we need for example for TRandom3
Definition: Monitor.cpp:372

◆ ThreadID()

static std::size_t M3::rand::Random::ThreadID ( )
inlinestaticprivate

Get the current OpenMP thread ID.

Definition at line 95 of file Random.h.

95  {
96  #ifdef MULTITHREAD
97  return static_cast<std::size_t>(omp_get_thread_num());
98  #else
99  return 0;
100  #endif
101  }

◆ Uniform()

double M3::rand::Random::Uniform ( const double  min = 0.0,
const double  max = 1.0 
)
inline

Generate a uniform random number in [min, max).

Definition at line 60 of file Random.h.

61  {
62  return Engine()->Uniform(min, max);
63  }

◆ UniformInt()

int M3::rand::Random::UniformInt ( const int  min,
const int  max 
)
inline

Generate a uniform random integer in [min, max].

Definition at line 66 of file Random.h.

66  {
67  return Engine()->Integer(max - min + 1) + min;
68  }

Member Data Documentation

◆ fEngines

std::vector<std::unique_ptr<TRandom3> > M3::rand::Random::fEngines
private

One random generator per thread.

Definition at line 107 of file Random.h.

◆ fSeed

std::uint64_t M3::rand::Random::fSeed = 0
private

The global seed.

Definition at line 104 of file Random.h.


The documentation for this class was generated from the following file: