Random.cxx
Go to the documentation of this file.
1 /*
2  Implementation notes:
3 
4  Implementation of Random uses actually another indirection
5  (pointer-to-implementation pattern). This is done in order to allow
6  for use of different C++ std engines and because the standard does
7  not deem it necessary to have these engines follow an inheritance
8  hierarchy.
9 
10  */
11 
12 #include "WireCellGen/Random.h"
13 
15 #include "WireCellUtil/Logging.h"
16 
17 #include <random>
18 
21 
22 using namespace WireCell;
23 using spdlog::warn;
24 
25 Gen::Random::Random(const std::string& generator,
26  const std::vector<unsigned int> seeds)
27  : m_generator(generator)
28  , m_seeds(seeds.begin(), seeds.end())
29  , m_pimpl(nullptr)
30 {
31 }
32 
33 
34 // This pimpl may turn out to be a bottle neck.
35 template<typename URNG>
36 class RandomT : public IRandom {
37  URNG m_rng;
38 public:
39  RandomT(std::vector<unsigned int> seeds) {
40  std::seed_seq seed(seeds.begin(), seeds.end());
41  m_rng.seed(seed);
42  }
43 
44  virtual int binomial(int max, double prob) {
45  std::binomial_distribution<int> distribution(max, prob);
46  return distribution(m_rng);
47  }
48  virtual int poisson(double mean) {
49  std::poisson_distribution<int> distribution(mean);
50  return distribution(m_rng);
51  }
52  virtual double normal(double mean, double sigma) {
53  std::normal_distribution<double> distribution(mean, sigma);
54  return distribution(m_rng);
55 
56  }
57  virtual double uniform(double begin, double end) {
58  std::uniform_real_distribution<double> distribution(begin, end);
59  return distribution(m_rng);
60  }
61  virtual double exponential(double mean) {
62  std::exponential_distribution<double> distribution(mean);
63  return distribution(m_rng);
64  }
65  virtual int range(int first, int last) {
66  std::uniform_int_distribution<int> distribution(first, last);
67  return distribution(m_rng);
68  }
69 };
70 
72 {
73  auto jseeds = cfg["seeds"];
74  if (not jseeds.isNull()) {
75  std::vector<unsigned int> seeds;
76  for (auto jseed : jseeds) {
77  seeds.push_back(jseed.asInt());
78  }
79  m_seeds = seeds;
80  }
81  auto gen = get(cfg,"generator",m_generator);
82  if (m_pimpl) {
83  delete m_pimpl;
84  }
85  if (gen == "default") {
86  m_pimpl = new RandomT<std::default_random_engine>(m_seeds);
87  }
88  else if (gen == "twister") {
89  m_pimpl = new RandomT<std::mt19937>(m_seeds);
90  }
91  else {
92  warn("Gen::Random::configure: warning: unknown random engine: \"{}\" using default", gen);
93  m_pimpl = new RandomT<std::default_random_engine>(m_seeds);
94  }
95 }
96 
97 WireCell::Configuration Gen::Random::default_configuration() const
98 {
100  cfg["generator"] = m_generator;
101  Json::Value jseeds(Json::arrayValue);
102  for (auto seed : m_seeds) {
103  jseeds.append(seed);
104  }
105  cfg["seeds"] = jseeds;
106  return cfg;
107 }
108 
109 int Gen::Random::binomial(int max, double prob)
110 {
111  return m_pimpl->binomial(max, prob);
112 }
113 int Gen::Random::poisson(double mean)
114 {
115  return m_pimpl->poisson(mean);
116 }
117 
118 double Gen::Random::normal(double mean, double sigma)
119 {
120  return m_pimpl->normal(mean, sigma);
121 }
122 
123 double Gen::Random::uniform(double begin, double end)
124 {
125  return m_pimpl->uniform(begin, end);
126 }
127 
128 double Gen::Random::exponential(double mean)
129 {
130  return m_pimpl->exponential(mean);
131 }
132 
133 int Gen::Random::range(int first, int last)
134 {
135  return m_pimpl->range(first, last);
136 }
137 
virtual int binomial(int max, double prob)
Definition: Random.cxx:44
RandomT(std::vector< unsigned int > seeds)
Definition: Random.cxx:39
virtual double uniform(double begin, double end)
Definition: Random.cxx:57
virtual double exponential(double mean)
Definition: Random.cxx:61
STL namespace.
cfg
Definition: dbjson.py:29
Definition: async.h:27
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
std::vector< pointer > vector
Vector of shared pointers.
Definition: IComponent.h:36
def configure(cfg)
Definition: cuda.py:34
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
WIRECELL_FACTORY(Random, WireCell::Gen::Random, WireCell::IRandom, WireCell::IConfigurable) using namespace WireCell
static int max(int a, int b)
URNG m_rng
Definition: Random.cxx:37
generator
Definition: train.py:468
Definition: Main.h:22
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:13
virtual int poisson(double mean)
Definition: Random.cxx:48
Json::Value Configuration
Definition: Configuration.h:50
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:67
virtual int range(int first, int last)
Definition: Random.cxx:65
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:15
virtual double normal(double mean, double sigma)
Definition: Random.cxx:52