-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random.h
158 lines (135 loc) · 3.78 KB
/
Random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#pragma once
#include <memory>
#include <random>
#include <gsl/gsl_rng.h>
namespace EERAModel {
/**
* @brief Namespace containing objects related to random number generation
*
* This namespace contains a wrapper interace to existing random number
* generation libraries in order for them to be used within all
* model functions.
*/
namespace Random {
/**
* @class RNGInterface
* @brief Abstract interface to a random number generator
*/
class RNGInterface {
public:
using Sptr = std::shared_ptr<RNGInterface>;
virtual ~RNGInterface() = default;
/**
* @brief Flat distribution
*
* Return a random number from a flat distribution
*
* @param a Lower limit of distribution interval
* @param b Upper limit of distribution interval
*
* @return Random number
*/
virtual double Flat(double a, double b) = 0;
/**
* @brief Multinomial distribution
*
* Return a random number from a multinomial distribution
*/
virtual void Multinomial(size_t K, unsigned int N, const double p[], unsigned int n[]) = 0;
/**
* @brief Poisson distribution
*
* Return a random number from a Poisson distribution
*
* @param mu Mean of the distribution
*
* @return Random number
*/
virtual double Poisson(double mu) = 0;
/**
* @brief Gamma distribution
*
* Return a random number from a Gamma distribution
*
* @param a Gamma a parameter
* @param b Gamma b parameter
*
* @return Random number
*/
virtual double Gamma(double a, double b) = 0;
/**
* @brief Beta distribution
*
* Return a random number from a Beta distribution
*
* @param a Beta a parameter
* @param b Beta b parameter
*
* @return Random number
*/
virtual double Beta(double a, double b) = 0;
/**
* @brief Binomial distribution
*
* Return a random number from a Binomial distribution
*
* @param p Binomial p parameter: probability
* @param n Binomial n parameter: the population to sample
*
* @return Random number
*/
virtual unsigned int Binomial(double p, unsigned int n) = 0;
/**
* @brief MT19937 generator
*
* Returns a reference to a std::mt19937 object
*
* @return mt19937 reference
*/
virtual std::mt19937& MT19937() = 0;
};
/**
* @class RNG
* @brief Wrapper for the GNU Scientific Library random number generator
*/
class RNG : public RNGInterface
{
public:
using Sptr = std::shared_ptr<RNG>;
/**
* @brief Contructor
*
* Sets up a handle for the underlying GSL and STL random number generator. Seeds the randomiser
* with the supplied seed
*
* @param seed Seed value
*/
RNG(unsigned long int seed);
/** @brief Flat distribution override */
virtual double Flat(double a, double b) override;
/** @brief Multinomial distribution override */
virtual void Multinomial(size_t K, unsigned int N, const double p[], unsigned int n[]) override;
/** @brief Poisson distribution override */
virtual double Poisson(double mu) override;
/** @brief Gamma distribution override */
virtual double Gamma(double a, double b) override;
/** @brief Beta distribution override */
virtual double Beta(double a, double b) override;
/** @brief Binomial distribution override */
virtual unsigned int Binomial(double p, unsigned int n) override;
/** @brief MT19937 override */
virtual std::mt19937& MT19937() override;
private:
/**
* @private
* @brief Handle for the GSL random number generator
*/
gsl_rng* r_;
/**
* @private
* @brief Mersenne twister generator
*/
std::mt19937 gen_;
};
} // namespace Random
} // namespace EERAModel