1 | // $Id: random.cc 1000 2007-12-23 20:09:15Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2007 Jari Häkkinen |
---|
6 | |
---|
7 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "random.h" |
---|
26 | #include "yat/statistics/Histogram.h" |
---|
27 | #include "yat/utility/Exception.h" |
---|
28 | |
---|
29 | #include <fstream> |
---|
30 | #include <sstream> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace random { |
---|
35 | |
---|
36 | RNG* RNG::instance_ = NULL; |
---|
37 | |
---|
38 | RNG::RNG(void) |
---|
39 | { |
---|
40 | // support rng/seed changes through environment vars |
---|
41 | if (!gsl_rng_env_setup()) |
---|
42 | throw utility::GSL_error("RNG::RNG unknown generator"); |
---|
43 | if (!(rng_=gsl_rng_alloc(gsl_rng_default))) |
---|
44 | throw utility::GSL_error("RNG::RNG failed to allocate memory"); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | RNG::~RNG(void) |
---|
49 | { |
---|
50 | gsl_rng_free(rng_); |
---|
51 | rng_=NULL; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | RNG* RNG::instance(void) |
---|
56 | { |
---|
57 | if (!instance_) |
---|
58 | instance_=new RNG; |
---|
59 | return instance_; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | u_long RNG::max(void) const |
---|
64 | { |
---|
65 | return gsl_rng_max(rng_); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | u_long RNG::min(void) const |
---|
70 | { |
---|
71 | return gsl_rng_min(rng_); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | std::string RNG::name(void) const |
---|
76 | { |
---|
77 | return gsl_rng_name(rng_); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | const gsl_rng* RNG::rng(void) const |
---|
82 | { |
---|
83 | return rng_; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | void RNG::seed(u_long s) const |
---|
88 | { |
---|
89 | gsl_rng_set(rng_,s); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | u_long RNG::seed_from_devurandom(void) |
---|
94 | { |
---|
95 | u_char ulongsize=sizeof(u_long); |
---|
96 | char* buffer=new char[ulongsize]; |
---|
97 | std::ifstream is("/dev/urandom"); |
---|
98 | is.read(buffer,ulongsize); |
---|
99 | is.close(); |
---|
100 | u_long s=0; |
---|
101 | for (u_int i=0; i<ulongsize; i++) { |
---|
102 | u_char ch=buffer[i]; |
---|
103 | s+=ch<<((ulongsize-1-i)*8); |
---|
104 | } |
---|
105 | seed(s); |
---|
106 | return s; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | int RNG::set_state(const RNG_state& state) |
---|
111 | { |
---|
112 | return gsl_rng_memcpy(rng_, state.rng()); |
---|
113 | } |
---|
114 | |
---|
115 | // --------------------- RNG_state ---------------------------------- |
---|
116 | |
---|
117 | RNG_state::RNG_state(const RNG* rng) |
---|
118 | { |
---|
119 | if (!(rng_ = gsl_rng_clone(rng->rng()))) |
---|
120 | throw utility::GSL_error("RNG_state::RNG_state failed to allocate memory"); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | RNG_state::~RNG_state(void) |
---|
125 | { |
---|
126 | gsl_rng_free(rng_); |
---|
127 | rng_=NULL; |
---|
128 | } |
---|
129 | |
---|
130 | const gsl_rng* RNG_state::rng(void) const |
---|
131 | { |
---|
132 | return rng_; |
---|
133 | } |
---|
134 | |
---|
135 | // --------------------- Discrete distribtuions --------------------- |
---|
136 | |
---|
137 | Discrete::Discrete(void) |
---|
138 | : rng_(RNG::instance()) |
---|
139 | { |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | Discrete::~Discrete(void) |
---|
144 | { |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | void Discrete::seed(u_long s) const |
---|
149 | { |
---|
150 | rng_->seed(s); |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist) |
---|
155 | { |
---|
156 | double* p = new double[ hist.nof_bins() ]; |
---|
157 | for (size_t i=0; i<hist.nof_bins(); i++) |
---|
158 | p[ i ] = hist[i]; |
---|
159 | gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p ); |
---|
160 | delete p; |
---|
161 | if (!gen_) |
---|
162 | throw utility::GSL_error("DiscreteGeneral::DiscreteGeneral failed to setup generator."); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | DiscreteGeneral::~DiscreteGeneral(void) |
---|
167 | { |
---|
168 | if (gen_) |
---|
169 | gsl_ran_discrete_free( gen_ ); |
---|
170 | gen_ = NULL; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | u_long DiscreteGeneral::operator()(void) const |
---|
175 | { |
---|
176 | return gsl_ran_discrete(rng_->rng(), gen_); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | DiscreteUniform::DiscreteUniform(u_long n) |
---|
181 | : range_(n) |
---|
182 | { |
---|
183 | if (range_>rng_->max()) { |
---|
184 | std::stringstream ss("DiscreteUniform::DiscreteUniform: "); |
---|
185 | ss << n << " is too large for RNG " << rng_->name(); |
---|
186 | throw utility::GSL_error(ss.str()); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | u_long DiscreteUniform::operator()(void) const |
---|
192 | { |
---|
193 | return (range_ ? |
---|
194 | gsl_rng_uniform_int(rng_->rng(), range_) : gsl_rng_get(rng_->rng())); |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | u_long DiscreteUniform::operator()(u_long n) const |
---|
199 | { |
---|
200 | // making sure that n is not larger than the range of the |
---|
201 | // underlying RNG |
---|
202 | if (n>rng_->max()) { |
---|
203 | std::stringstream ss("DiscreteUniform::operator(u_long): "); |
---|
204 | ss << n << " is too large for RNG " << rng_->name(); |
---|
205 | throw utility::GSL_error(ss.str()); |
---|
206 | } |
---|
207 | return gsl_rng_uniform_int(rng_->rng(),n); |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | Poisson::Poisson(const double m) |
---|
212 | : m_(m) |
---|
213 | { |
---|
214 | } |
---|
215 | |
---|
216 | u_long Poisson::operator()(void) const |
---|
217 | { |
---|
218 | return gsl_ran_poisson(rng_->rng(), m_); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | u_long Poisson::operator()(const double m) const |
---|
223 | { |
---|
224 | return gsl_ran_poisson(rng_->rng(), m); |
---|
225 | } |
---|
226 | |
---|
227 | // --------------------- Continuous distribtuions --------------------- |
---|
228 | |
---|
229 | Continuous::Continuous(void) |
---|
230 | : rng_(RNG::instance()) |
---|
231 | { |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | Continuous::~Continuous(void) |
---|
236 | { |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | void Continuous::seed(u_long s) const |
---|
241 | { |
---|
242 | rng_->seed(s); |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | ContinuousGeneral::ContinuousGeneral(const statistics::Histogram& hist) |
---|
247 | : discrete_(DiscreteGeneral(hist)), hist_(hist) |
---|
248 | { |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | double ContinuousGeneral::operator()(void) const |
---|
253 | { |
---|
254 | return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing(); |
---|
255 | } |
---|
256 | |
---|
257 | double ContinuousUniform::operator()(void) const |
---|
258 | { |
---|
259 | return gsl_rng_uniform(rng_->rng()); |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | Exponential::Exponential(const double m) |
---|
264 | : m_(m) |
---|
265 | { |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | double Exponential::operator()(void) const |
---|
270 | { |
---|
271 | return gsl_ran_exponential(rng_->rng(), m_); |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | double Exponential::operator()(const double m) const |
---|
276 | { |
---|
277 | return gsl_ran_exponential(rng_->rng(), m); |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | Gaussian::Gaussian(const double s, const double m) |
---|
282 | : m_(m), s_(s) |
---|
283 | { |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | double Gaussian::operator()(void) const |
---|
288 | { |
---|
289 | return gsl_ran_gaussian(rng_->rng(), s_)+m_; |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | double Gaussian::operator()(const double s) const |
---|
294 | { |
---|
295 | return gsl_ran_gaussian(rng_->rng(), s); |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | double Gaussian::operator()(const double s, const double m) const |
---|
300 | { |
---|
301 | return gsl_ran_gaussian(rng_->rng(), s)+m; |
---|
302 | } |
---|
303 | |
---|
304 | }}} // of namespace random, yat, and theplu |
---|