CYCLUS
Loading...
Searching...
No Matches
random_number_generator.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_RNG_H
2#define CYCLUS_SRC_RNG_H
3
4#include <boost/random.hpp>
5#include <cstdint>
6
7#include "error.h"
8
9class SimInitTest;
10class RandomTest;
11
12namespace cyclus {
13
14class SimInfo;
15
16typedef boost::random::mt19937 Generator;
17typedef boost::random::variate_generator<Generator&, boost::random::normal_distribution<> > NormalDist;
18
19/// A random number generator.
21 friend class ::SimInitTest;
22 friend class ::RandomTest;
23
24 // all distributions are fiends
25 friend class FixedDoubleDist;
26 friend class UniformDoubleDist;
27 friend class NormalDoubleDist;
28 friend class FixedIntDist;
29 friend class UniformIntDist;
30 friend class NormalIntDist;
31
32 private:
33 /// Returns a random number for use in a distribution
34 static Generator gen_;
35
36 public:
37 /// Initialize from seed
38 void Initialize(SimInfo si);
39
40 std::uint32_t random();
41
42 // in archetype code
43 //
44 // #include "random_number_generator.h"
45 //
46 // class Foo:
47 // private:
48 // NormalDist myNormalDist;
49 //
50 // Foo::Foo() {
51 // myNormalDist = RandomNumberGenerator::makeNormalDist(mean, std_dev, min, max);
52 // }
53 //
54 // void Foo::Tick() {
55 // bar = myNormalDist();
56 // }
57 //
58
59 static NormalDist makeNormalDist(double mean, double std_dev, double min, double max) {
60 boost::random::normal_distribution<> dist(mean, std_dev);
61 NormalDist rn(gen_, dist);
62
63 return rn;
64 }
65
66 /// wrappers for boost::random distributions
67
68 /// geenerate a random number between [0, 1)
69 double random_01();
70
71 /// generate a random integer between [low, high)
72 int random_uniform_int(int low, int high);
73
74 /// generate a random real number between [low, high)
75 double random_uniform_real(double low, double high);
76
77 /// generate a double from a normal distribution, with truncation
78 /// at low and high
79 double random_normal_real(double mean, double std_dev, double low=0,
80 double high=std::numeric_limits<double>::max());
81
82 /// generates an integer from a normal distribution, with truncation
83 /// uses rounding to convert double to int
84 int random_normal_int(double mean, double std_dev, int low=0,
85 int high=std::numeric_limits<int>::max());
86
87};
88
90 public:
91 typedef boost::shared_ptr<DoubleDistribution> Ptr;
92
93 virtual double sample() = 0;
94 virtual double max() = 0;
95};
96
98 private:
99 double value;
100 public:
101 typedef boost::shared_ptr<FixedDoubleDist> Ptr;
102
103 FixedDoubleDist(double value_) : value(value_) {};
104 virtual double sample() { return value; };
105 virtual double max() { return value; };
106};
107
109 private:
110 boost::random::uniform_real_distribution<> dist;
111 public:
112 typedef boost::shared_ptr<UniformDoubleDist> Ptr;
113
114 UniformDoubleDist(double min = 0, double max=1) : dist(min, max) {};
115 virtual double sample() { return dist(RandomNumberGenerator::gen_); }
116 virtual double max() { return dist.max(); }
117};
118
120 private:
121 boost::random::normal_distribution<> dist;
122 double min_;
123 double max_;
124 public:
125 typedef boost::shared_ptr<NormalDoubleDist> Ptr;
126
127 NormalDoubleDist(double mean, double std_dev, double min=0, double max=1) : dist(mean, std_dev), min_(min), max_(max) {
128 if (min_ == max_) {
129 throw ValueError("Min and max cannot be equal for a normal distribution. Either use FixedDoubleDist or change the min/max.");
130 }
131 if (max_ < (mean - 3*std_dev) || min_ > (mean + 3*std_dev)) {
132 Warn<VALUE_WARNING>("Dist is sampling from a tail of a truncated normal more than 3 standard deviations from the mean. Drawing sampling may be inefficient");
133 }
134 };
135 virtual double sample();
136 virtual double max() { return max_; }
137};
138
140 public:
141 typedef boost::shared_ptr<IntDistribution> Ptr;
142 virtual int sample() = 0;
143};
144
146 private:
147 int value;
148 public:
149 typedef boost::shared_ptr<FixedIntDist> Ptr;
150
151 FixedIntDist(int value_) : value(value_) {};
152 virtual int sample() { return value; };
153};
154
156 private:
157 boost::random::uniform_int_distribution<> dist;
158 public:
159 typedef boost::shared_ptr<UniformIntDist> Ptr;
160
161 UniformIntDist(int min = 0, int max=1) : dist(min, max) {};
162 virtual int sample() { return dist(RandomNumberGenerator::gen_); }
163 virtual int max() { return dist.max(); }
164};
165
167 private:
168 boost::random::normal_distribution<> dist;
169 int min_;
170 int max_;
171 public:
172 typedef boost::shared_ptr<NormalIntDist> Ptr;
173
174 NormalIntDist(double mean, double std_dev, int min=0, int max=1) : dist(mean, std_dev), min_(min), max_(max) {
175 if (min_ == max_) {
176 throw ValueError("Min and max cannot be equal for a normal distribution. Either use FixedIntDist or change the min/max.");
177 }
178 if (max_ < (mean - 3*std_dev) || min_ > (mean + 3*std_dev)) {
179 Warn<VALUE_WARNING>("Dist is sampling from a tail of a truncated normal more than 3 standard deviations from the mean. Drawing sampling may be inefficient");
180 }
181 };
182 virtual int sample();
183 virtual int max() { return max_; }
184};
185
186}
187
188#endif // CYCLUS_SRC_RNG_H
virtual double max()=0
boost::shared_ptr< DoubleDistribution > Ptr
virtual double sample()=0
boost::shared_ptr< FixedDoubleDist > Ptr
boost::shared_ptr< FixedIntDist > Ptr
boost::shared_ptr< IntDistribution > Ptr
virtual int sample()=0
boost::shared_ptr< NormalDoubleDist > Ptr
NormalDoubleDist(double mean, double std_dev, double min=0, double max=1)
NormalIntDist(double mean, double std_dev, int min=0, int max=1)
boost::shared_ptr< NormalIntDist > Ptr
double random_01()
wrappers for boost::random distributions
int random_uniform_int(int low, int high)
generate a random integer between [low, high)
double random_uniform_real(double low, double high)
generate a random real number between [low, high)
void Initialize(SimInfo si)
Initialize from seed.
double random_normal_real(double mean, double std_dev, double low=0, double high=std::numeric_limits< double >::max())
generate a double from a normal distribution, with truncation at low and high
int random_normal_int(double mean, double std_dev, int low=0, int high=std::numeric_limits< int >::max())
generates an integer from a normal distribution, with truncation uses rounding to convert double to i...
static NormalDist makeNormalDist(double mean, double std_dev, double min, double max)
Container for a static simulation-global parameters that both describe the simulation and affect its ...
Definition context.h:45
boost::shared_ptr< UniformDoubleDist > Ptr
UniformDoubleDist(double min=0, double max=1)
boost::shared_ptr< UniformIntDist > Ptr
UniformIntDist(int min=0, int max=1)
For values that are too big, too small, etc.
Definition error.h:41
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
boost::random::mt19937 Generator
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
boost::random::variate_generator< Generator &, boost::random::normal_distribution<> > NormalDist