CbmRoot
Loading...
Searching...
No Matches
CaAlgoRandom.h
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#ifndef CaAlgoRandom_h
11#define CaAlgoRandom_h 1
12
13#include <Logger.h>
14
15#include <cstdint>
16#include <random>
17#include <type_traits>
18
19namespace cbm::algo::ca
20{
22 class Random {
23 using GeneratorType_t = std::mt19937_64;
24
25 public:
29 Random();
30
35 Random(int seed);
36
38 ~Random() = default;
39
41 Random(const Random& other) = delete;
42
44 Random(Random&& other) = delete;
45
47 Random& operator=(const Random&) = delete;
48
50 Random& operator=(Random&&) = delete;
51
53 int GetSeed() const { return fSeed; }
54
59 void SetSeed(int seed);
60
66 template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
67 T BoundedGaus(const T& mean, const T& sigma, const T& nSigmas) const;
68
73 template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
74 T Uniform(const T& mean, const T& sigma) const;
75
76
77 private:
79 unsigned int fSeed = 1;
80 };
81
82
83 // *********************************************************
84 // ** Template and inline function implementation **
85 // *********************************************************
86
87 // ---------------------------------------------------------------------------------------------------------------------
88 //
89 template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool>>
90 T Random::BoundedGaus(const T& mean, const T& sigma, const T& nSigmas) const
91 {
92 LOG_IF(fatal, !(nSigmas > 0 && std::isfinite(nSigmas))) << "ca::algo::Random::BoundedGaus nSigmas = " << nSigmas;
93 LOG_IF(fatal, !(sigma > 0 && std::isfinite(sigma))) << "ca::algo::Random::BoundedGaus sigma = " << sigma;
94
95 std::normal_distribution rndGaus{mean, sigma};
96 double res = 0;
97 do {
98 res = rndGaus(fGenerator);
99 } while (std::fabs(res - mean) > nSigmas * sigma);
100 return res;
101 }
102
103 // ---------------------------------------------------------------------------------------------------------------------
104 //
105 template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool>>
106 T Random::Uniform(const T& mean, const T& sigma) const
107 {
108 LOG_IF(fatal, !(sigma > 0 && std::isfinite(sigma)))
109 << "ca::algo::Random::Uniform sigma = " << sigma << " is illegal";
110 std::uniform_real_distribution rnd{-sigma * std::sqrt(3) + mean, sigma * std::sqrt(3) + mean};
111 return rnd(fGenerator);
112 }
113
114} // namespace cbm::algo::ca
115
116#endif // CaAlgoRandom_h
A class, providing ROOT-free access to randomly generated variables.
Random & operator=(Random &&)=delete
Move assignment operator.
T Uniform(const T &mean, const T &sigma) const
Returns a random value, addressed with a continuous uniform distribution.
int GetSeed() const
Gets seed of the random generator.
Random(const Random &other)=delete
Copy constructor.
Random & operator=(const Random &)=delete
Copy assignment operator.
GeneratorType_t fGenerator
Random number generator.
T BoundedGaus(const T &mean, const T &sigma, const T &nSigmas) const
Returns a normally distributed random value, limited within a selected sigma range.
unsigned int fSeed
Random number seed.
~Random()=default
Destructor.
Random(Random &&other)=delete
Move constructor.
Random()
Default constructor.
std::mt19937_64 GeneratorType_t
void SetSeed(int seed)
Sets seed to the random generator.
TODO: SZh 8.11.2022: add selection of parameterisation.
Definition CaBranch.h:14