1 | // $Id: Random.h 365 2005-08-05 08:32:13Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_random_ |
---|
4 | #define _theplu_random_ |
---|
5 | |
---|
6 | |
---|
7 | #include <gsl/gsl_rng.h> |
---|
8 | #include <gsl/gsl_randist.h> |
---|
9 | |
---|
10 | namespace theplu { |
---|
11 | namespace random { |
---|
12 | |
---|
13 | /// |
---|
14 | /// The RNG class provides a single global random number generator |
---|
15 | /// instance with one point of access to the generator. |
---|
16 | /// |
---|
17 | /// This is probably not thread safe. |
---|
18 | /// |
---|
19 | /// @see Design Patterns (the singleton and adapter pattern) |
---|
20 | /// |
---|
21 | class RNG |
---|
22 | { |
---|
23 | public: |
---|
24 | |
---|
25 | virtual ~RNG(void); |
---|
26 | |
---|
27 | static RNG* instance(int seed); |
---|
28 | |
---|
29 | inline const gsl_rng* rng(void) const { return rng_; } |
---|
30 | |
---|
31 | private: |
---|
32 | |
---|
33 | RNG(void); |
---|
34 | |
---|
35 | static RNG* instance_; |
---|
36 | gsl_rng* rng_; |
---|
37 | }; |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | class RandomContinuous |
---|
42 | { |
---|
43 | public: |
---|
44 | |
---|
45 | inline RandomContinuous(void) { rng_=RNG::instance(89); } |
---|
46 | |
---|
47 | virtual double operator()(void) const = 0; |
---|
48 | |
---|
49 | protected: |
---|
50 | RNG* rng_; |
---|
51 | }; |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | class RandomContinuousUniform : public RandomContinuous |
---|
56 | { |
---|
57 | public: |
---|
58 | |
---|
59 | inline double operator()(void) const { return gsl_rng_uniform(rng_->rng());} |
---|
60 | |
---|
61 | }; |
---|
62 | |
---|
63 | /// |
---|
64 | /// Class to generate Gaussian random numbers. |
---|
65 | /// |
---|
66 | class RandomGaussian : public RandomContinuous |
---|
67 | { |
---|
68 | public: |
---|
69 | inline RandomGaussian(const double s=1, const double m=0) |
---|
70 | : m_(m), s_(s) {} |
---|
71 | |
---|
72 | inline double operator()(void) const |
---|
73 | { return gsl_ran_gaussian(rng_->rng(), s_)+m_; } |
---|
74 | |
---|
75 | inline double operator()(const double s) const |
---|
76 | { return gsl_ran_gaussian(rng_->rng(), s); } |
---|
77 | |
---|
78 | inline double operator()(const double s, const double m) const |
---|
79 | { return gsl_ran_gaussian(rng_->rng(), s)+m; } |
---|
80 | |
---|
81 | private: |
---|
82 | double m_; |
---|
83 | double s_; |
---|
84 | }; |
---|
85 | |
---|
86 | /// |
---|
87 | /// @brief exponential |
---|
88 | /// |
---|
89 | class RandomExponential : public RandomContinuous |
---|
90 | { |
---|
91 | public: |
---|
92 | /// |
---|
93 | /// Constructor |
---|
94 | /// |
---|
95 | inline RandomExponential(const double m=1) |
---|
96 | : m_(m){} |
---|
97 | |
---|
98 | /// |
---|
99 | /// |
---|
100 | /// |
---|
101 | inline double operator()(void) const |
---|
102 | { return gsl_ran_exponential(rng_->rng(), m_); } |
---|
103 | |
---|
104 | /// |
---|
105 | /// |
---|
106 | /// |
---|
107 | inline double operator()(const double m) const |
---|
108 | { return gsl_ran_exponential(rng_->rng(), m); } |
---|
109 | |
---|
110 | private: |
---|
111 | double m_; |
---|
112 | }; |
---|
113 | |
---|
114 | |
---|
115 | /// |
---|
116 | /// Base class for discrete random distributions. |
---|
117 | /// |
---|
118 | class RandomDiscrete |
---|
119 | { |
---|
120 | public: |
---|
121 | /// |
---|
122 | /// Constructor |
---|
123 | /// |
---|
124 | inline RandomDiscrete(void) { rng_=RNG::instance(89); } |
---|
125 | |
---|
126 | /// |
---|
127 | /// Common interface for all Discrete Random Classes |
---|
128 | /// |
---|
129 | virtual long operator()(void) const = 0; |
---|
130 | |
---|
131 | protected: |
---|
132 | RNG* rng_; |
---|
133 | }; |
---|
134 | |
---|
135 | |
---|
136 | class RandomDiscreteUniform : public RandomDiscrete |
---|
137 | { |
---|
138 | public: |
---|
139 | /// |
---|
140 | /// Constructor. |
---|
141 | /// |
---|
142 | inline RandomDiscreteUniform(const u_long range, const long min=0) |
---|
143 | : min_(min), range_(range) {} |
---|
144 | |
---|
145 | /// |
---|
146 | /// @return A random number between \a min_ and \a range_ - \a min_ |
---|
147 | /// |
---|
148 | inline long operator()(void) const |
---|
149 | { return gsl_rng_uniform_int(rng_->rng(), range_)+min_; } |
---|
150 | |
---|
151 | /// |
---|
152 | /// @return A random number between 0 and \a range. |
---|
153 | /// |
---|
154 | inline long operator()(const u_long range) const |
---|
155 | { return gsl_rng_uniform_int(rng_->rng(), range); } |
---|
156 | |
---|
157 | /// |
---|
158 | /// @return A random number between \a min and \a range - \a min |
---|
159 | /// |
---|
160 | inline long operator()(const u_long range, const long min) const |
---|
161 | { return gsl_rng_uniform_int(rng_->rng(), range)+min; } |
---|
162 | |
---|
163 | |
---|
164 | private: |
---|
165 | long min_; |
---|
166 | u_long range_; |
---|
167 | }; |
---|
168 | |
---|
169 | |
---|
170 | /// |
---|
171 | /// @brief Poisson |
---|
172 | /// |
---|
173 | class RandomPoisson : public RandomContinuous |
---|
174 | { |
---|
175 | public: |
---|
176 | /// |
---|
177 | /// Constructor |
---|
178 | /// |
---|
179 | inline RandomPoisson(const double m=1) |
---|
180 | : m_(m){} |
---|
181 | |
---|
182 | /// |
---|
183 | /// |
---|
184 | /// |
---|
185 | inline double operator()(void) const |
---|
186 | { return gsl_ran_poisson(rng_->rng(), m_); } |
---|
187 | |
---|
188 | /// |
---|
189 | /// |
---|
190 | /// |
---|
191 | inline double operator()(const double m) const |
---|
192 | { return gsl_ran_poisson(rng_->rng(), m); } |
---|
193 | |
---|
194 | private: |
---|
195 | double m_; |
---|
196 | }; |
---|
197 | |
---|
198 | /// |
---|
199 | /// @brief General |
---|
200 | /// |
---|
201 | class RandomDiscreteGeneral : public RandomDiscrete { |
---|
202 | }; |
---|
203 | |
---|
204 | |
---|
205 | }} // of namespace random and namespace theplu |
---|
206 | |
---|
207 | #endif |
---|