source: trunk/yat/random/random.cc @ 1706

Last change on this file since 1706 was 1616, checked in by Peter, 13 years ago

fixes #459

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1// $Id: random.cc 1616 2008-11-05 21:36:48Z 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "random.h"
24#include "yat/statistics/Histogram.h"
25#include "yat/utility/Exception.h"
26
27#include <cassert>
28#include <fstream>
29#include <sstream>
30
31namespace theplu {
32namespace yat {
33namespace random {
34
35  RNG* RNG::instance_ = NULL;
36
37  RNG::RNG(void)
38  {
39      // support rng/seed changes through environment vars
40    if (!gsl_rng_env_setup())
41      throw utility::GSL_error("RNG::RNG unknown generator");
42    if (!(rng_=gsl_rng_alloc(gsl_rng_default)))
43      throw utility::GSL_error("RNG::RNG failed to allocate memory");
44  }
45
46
47  RNG::~RNG(void)
48  {
49    gsl_rng_free(rng_);
50    rng_=NULL;
51  }
52
53 
54  RNG* RNG::instance(void)
55  {
56    if (!instance_)
57      instance_=new RNG;
58    return instance_;
59  }
60
61
62  unsigned long RNG::max(void) const
63  {
64    return gsl_rng_max(rng_);
65  }
66
67
68  unsigned long RNG::min(void) const
69  {
70    return gsl_rng_min(rng_);
71  }
72
73
74  std::string RNG::name(void) const
75  {
76    return gsl_rng_name(rng_);
77  }
78
79
80  const gsl_rng* RNG::rng(void) const
81  {
82    return rng_;
83  }
84
85
86  void RNG::seed(unsigned long s) const
87  {
88    gsl_rng_set(rng_,s);
89  }
90
91
92  unsigned long RNG::seed_from_devurandom(void)
93  {
94    unsigned char ulongsize=sizeof(unsigned long);
95    char* buffer=new char[ulongsize];
96    std::ifstream is("/dev/urandom");
97    is.read(buffer,ulongsize);
98    is.close();
99    unsigned long s=0;
100    for (unsigned int i=0; i<ulongsize; i++) {
101      unsigned char ch=buffer[i];
102      s+=ch<<((ulongsize-1-i)*8);
103    }
104    seed(s);
105    return s;
106  }
107
108
109  int RNG::set_state(const RNG_state& state)
110  {
111    return gsl_rng_memcpy(rng_, state.rng());
112  }
113
114  // --------------------- RNG_state ----------------------------------
115
116  RNG_state::RNG_state(const RNG* rng)
117  {
118    clone(*rng->rng());
119  }
120 
121
122  RNG_state::RNG_state(const RNG_state& state)
123  {
124    clone(*state.rng());
125  }
126 
127
128  RNG_state::~RNG_state(void)
129  {
130    gsl_rng_free(rng_);
131    rng_=NULL;
132  }
133
134  const gsl_rng* RNG_state::rng(void) const
135  {
136    return rng_;
137  }
138
139
140  void RNG_state::clone(const gsl_rng& rng)
141  {
142    assert(rng_!=&rng);
143    if (!(rng_ = gsl_rng_clone(&rng)))
144      throw utility::GSL_error("RNG_state::RNG_state failed to allocate memory");
145  }
146
147  RNG_state& RNG_state::operator=(const RNG_state& rhs)
148  {
149    if (this != &rhs) {
150      gsl_rng_free(rng_);
151      clone(*rhs.rng());
152    }
153    return *this;
154  }
155
156  // --------------------- Discrete distribtuions ---------------------
157
158  Discrete::Discrete(void)
159    : rng_(RNG::instance())
160  {
161  }
162
163 
164  Discrete::~Discrete(void)
165  {
166  }
167
168
169  void Discrete::seed(unsigned long s) const
170  {
171    rng_->seed(s);
172  }
173
174
175  unsigned long Discrete::seed_from_devurandom(void) 
176  { 
177    return rng_->seed_from_devurandom(); 
178  }
179
180
181  DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist)
182    : gen_(NULL)
183  {
184    p_.reserve(hist.nof_bins());
185    for (size_t i=0; i<hist.nof_bins(); i++) 
186      p_.push_back(hist[i]);
187    preproc();
188  }
189
190
191  DiscreteGeneral::DiscreteGeneral(const DiscreteGeneral& other)
192    : gen_(NULL), p_(other.p_)
193  {
194    preproc();
195  }
196
197
198  DiscreteGeneral::~DiscreteGeneral(void)
199  {
200    free();
201  }
202
203
204  void DiscreteGeneral::free(void)
205  {
206    if (gen_)
207      gsl_ran_discrete_free( gen_ );
208    gen_ = NULL;
209  }
210
211
212  void DiscreteGeneral::preproc(void)
213  {
214    assert(!gen_);
215    assert(p_.size());
216    gen_ = gsl_ran_discrete_preproc( p_.size(), &p_.front() );
217    if (!gen_)
218      throw utility::GSL_error("DiscreteGeneral failed to setup generator.");
219  }
220
221
222  DiscreteGeneral& DiscreteGeneral::operator=(const DiscreteGeneral& rhs)
223  {
224    free();
225    p_ = rhs.p_;
226    preproc();
227    return *this;
228  }
229
230
231  unsigned long DiscreteGeneral::operator()(void) const
232  {
233    return gsl_ran_discrete(rng_->rng(), gen_);
234  }
235
236
237  DiscreteUniform::DiscreteUniform(unsigned long n)
238    : range_(n)
239  {
240    if (range_>rng_->max()) {
241      std::stringstream ss("DiscreteUniform::DiscreteUniform: ");
242      ss << n << " is too large for RNG " << rng_->name();
243      throw utility::GSL_error(ss.str());
244    }
245  }
246
247
248  unsigned long DiscreteUniform::operator()(void) const
249  {
250    return (range_ ?
251            gsl_rng_uniform_int(rng_->rng(), range_) : gsl_rng_get(rng_->rng()));
252  }
253
254
255  unsigned long DiscreteUniform::operator()(unsigned long n) const
256  {
257    // making sure that n is not larger than the range of the
258    // underlying RNG
259    if (n>rng_->max()) {
260      std::stringstream ss("DiscreteUniform::operator(unsigned long): ");
261      ss << n << " is too large for RNG " << rng_->name();
262      throw utility::GSL_error(ss.str());
263    }
264    return gsl_rng_uniform_int(rng_->rng(),n);
265  }
266
267
268  Poisson::Poisson(const double m)
269    : m_(m)
270  {
271  }
272
273  unsigned long Poisson::operator()(void) const
274  {
275    return gsl_ran_poisson(rng_->rng(), m_);
276  }
277
278
279  unsigned long Poisson::operator()(const double m) const
280  {
281    return gsl_ran_poisson(rng_->rng(), m);
282  }
283
284  // --------------------- Continuous distribtuions ---------------------
285
286  Continuous::Continuous(void)
287    : rng_(RNG::instance())
288  {
289  }
290
291
292  Continuous::~Continuous(void)
293  {
294  }
295
296
297  void Continuous::seed(unsigned long s) const
298  {
299    rng_->seed(s);
300  }
301
302
303  ContinuousGeneral::ContinuousGeneral(const statistics::Histogram& hist)
304    : discrete_(DiscreteGeneral(hist)), hist_(hist)
305  {
306  }
307
308
309  double ContinuousGeneral::operator()(void) const
310  {
311    return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing();
312  }
313
314  double ContinuousUniform::operator()(void) const
315  {
316    return gsl_rng_uniform(rng_->rng());
317  }
318
319
320  Exponential::Exponential(const double m)
321    : m_(m)
322  {
323  }
324
325
326  double Exponential::operator()(void) const
327  {
328    return gsl_ran_exponential(rng_->rng(), m_);
329  }
330
331
332  double Exponential::operator()(const double m) const
333  {
334    return gsl_ran_exponential(rng_->rng(), m);
335  }
336
337
338  Gaussian::Gaussian(const double s, const double m)
339    : m_(m), s_(s)
340  {
341  }
342
343
344  double Gaussian::operator()(void) const
345  {
346    return gsl_ran_gaussian(rng_->rng(), s_)+m_;
347  }
348
349
350  double Gaussian::operator()(const double s) const
351  {
352    return gsl_ran_gaussian(rng_->rng(), s);
353  }
354
355
356  double Gaussian::operator()(const double s, const double m) const
357  {
358    return gsl_ran_gaussian(rng_->rng(), s)+m;
359  }
360
361}}} // of namespace random, yat, and theplu
Note: See TracBrowser for help on using the repository browser.