1 | // $Id: random.cc 1437 2008-08-25 17:55:00Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.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 | unsigned long RNG::max(void) const |
---|
64 | { |
---|
65 | return gsl_rng_max(rng_); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | unsigned 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(unsigned long s) const |
---|
88 | { |
---|
89 | gsl_rng_set(rng_,s); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | unsigned long RNG::seed_from_devurandom(void) |
---|
94 | { |
---|
95 | unsigned char ulongsize=sizeof(unsigned long); |
---|
96 | char* buffer=new char[ulongsize]; |
---|
97 | std::ifstream is("/dev/urandom"); |
---|
98 | is.read(buffer,ulongsize); |
---|
99 | is.close(); |
---|
100 | unsigned long s=0; |
---|
101 | for (unsigned int i=0; i<ulongsize; i++) { |
---|
102 | unsigned 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(unsigned long s) const |
---|
149 | { |
---|
150 | rng_->seed(s); |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | unsigned long Discrete::seed_from_devurandom(void) |
---|
155 | { |
---|
156 | return rng_->seed_from_devurandom(); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist) |
---|
161 | { |
---|
162 | double* p = new double[ hist.nof_bins() ]; |
---|
163 | for (size_t i=0; i<hist.nof_bins(); i++) |
---|
164 | p[ i ] = hist[i]; |
---|
165 | gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p ); |
---|
166 | delete p; |
---|
167 | if (!gen_) |
---|
168 | throw utility::GSL_error("DiscreteGeneral::DiscreteGeneral failed to setup generator."); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | DiscreteGeneral::~DiscreteGeneral(void) |
---|
173 | { |
---|
174 | if (gen_) |
---|
175 | gsl_ran_discrete_free( gen_ ); |
---|
176 | gen_ = NULL; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | unsigned long DiscreteGeneral::operator()(void) const |
---|
181 | { |
---|
182 | return gsl_ran_discrete(rng_->rng(), gen_); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | DiscreteUniform::DiscreteUniform(unsigned long n) |
---|
187 | : range_(n) |
---|
188 | { |
---|
189 | if (range_>rng_->max()) { |
---|
190 | std::stringstream ss("DiscreteUniform::DiscreteUniform: "); |
---|
191 | ss << n << " is too large for RNG " << rng_->name(); |
---|
192 | throw utility::GSL_error(ss.str()); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | unsigned long DiscreteUniform::operator()(void) const |
---|
198 | { |
---|
199 | return (range_ ? |
---|
200 | gsl_rng_uniform_int(rng_->rng(), range_) : gsl_rng_get(rng_->rng())); |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | unsigned long DiscreteUniform::operator()(unsigned long n) const |
---|
205 | { |
---|
206 | // making sure that n is not larger than the range of the |
---|
207 | // underlying RNG |
---|
208 | if (n>rng_->max()) { |
---|
209 | std::stringstream ss("DiscreteUniform::operator(unsigned long): "); |
---|
210 | ss << n << " is too large for RNG " << rng_->name(); |
---|
211 | throw utility::GSL_error(ss.str()); |
---|
212 | } |
---|
213 | return gsl_rng_uniform_int(rng_->rng(),n); |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | Poisson::Poisson(const double m) |
---|
218 | : m_(m) |
---|
219 | { |
---|
220 | } |
---|
221 | |
---|
222 | unsigned long Poisson::operator()(void) const |
---|
223 | { |
---|
224 | return gsl_ran_poisson(rng_->rng(), m_); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | unsigned long Poisson::operator()(const double m) const |
---|
229 | { |
---|
230 | return gsl_ran_poisson(rng_->rng(), m); |
---|
231 | } |
---|
232 | |
---|
233 | // --------------------- Continuous distribtuions --------------------- |
---|
234 | |
---|
235 | Continuous::Continuous(void) |
---|
236 | : rng_(RNG::instance()) |
---|
237 | { |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | Continuous::~Continuous(void) |
---|
242 | { |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | void Continuous::seed(unsigned long s) const |
---|
247 | { |
---|
248 | rng_->seed(s); |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | ContinuousGeneral::ContinuousGeneral(const statistics::Histogram& hist) |
---|
253 | : discrete_(DiscreteGeneral(hist)), hist_(hist) |
---|
254 | { |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | double ContinuousGeneral::operator()(void) const |
---|
259 | { |
---|
260 | return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing(); |
---|
261 | } |
---|
262 | |
---|
263 | double ContinuousUniform::operator()(void) const |
---|
264 | { |
---|
265 | return gsl_rng_uniform(rng_->rng()); |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | Exponential::Exponential(const double m) |
---|
270 | : m_(m) |
---|
271 | { |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | double Exponential::operator()(void) const |
---|
276 | { |
---|
277 | return gsl_ran_exponential(rng_->rng(), m_); |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | double Exponential::operator()(const double m) const |
---|
282 | { |
---|
283 | return gsl_ran_exponential(rng_->rng(), m); |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | Gaussian::Gaussian(const double s, const double m) |
---|
288 | : m_(m), s_(s) |
---|
289 | { |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | double Gaussian::operator()(void) const |
---|
294 | { |
---|
295 | return gsl_ran_gaussian(rng_->rng(), s_)+m_; |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | double Gaussian::operator()(const double s) const |
---|
300 | { |
---|
301 | return gsl_ran_gaussian(rng_->rng(), s); |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | double Gaussian::operator()(const double s, const double m) const |
---|
306 | { |
---|
307 | return gsl_ran_gaussian(rng_->rng(), s)+m; |
---|
308 | } |
---|
309 | |
---|
310 | }}} // of namespace random, yat, and theplu |
---|