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

Last change on this file since 1614 was 1614, checked in by Peter, 15 years ago

implementing copying in RNG_state. Previously compiler generated versions were used which was not sound. Also disallowed assignment of singleton RNG. Assignment of a singleton class does not make sense.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1// $Id: random.cc 1614 2008-11-05 20:08:55Z 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  {
183    double* p = new double[ hist.nof_bins() ];
184    for (size_t i=0; i<hist.nof_bins(); i++) 
185      p[ i ] = hist[i];
186    gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p );
187    delete p;
188    if (!gen_)
189      throw utility::GSL_error("DiscreteGeneral::DiscreteGeneral failed to setup generator.");
190  }
191
192
193  DiscreteGeneral::~DiscreteGeneral(void)
194  {
195    if (gen_)
196      gsl_ran_discrete_free( gen_ );
197    gen_ = NULL;
198  }
199
200
201  unsigned long DiscreteGeneral::operator()(void) const
202  {
203    return gsl_ran_discrete(rng_->rng(), gen_);
204  }
205
206
207  DiscreteUniform::DiscreteUniform(unsigned long n)
208    : range_(n)
209  {
210    if (range_>rng_->max()) {
211      std::stringstream ss("DiscreteUniform::DiscreteUniform: ");
212      ss << n << " is too large for RNG " << rng_->name();
213      throw utility::GSL_error(ss.str());
214    }
215  }
216
217
218  unsigned long DiscreteUniform::operator()(void) const
219  {
220    return (range_ ?
221            gsl_rng_uniform_int(rng_->rng(), range_) : gsl_rng_get(rng_->rng()));
222  }
223
224
225  unsigned long DiscreteUniform::operator()(unsigned long n) const
226  {
227    // making sure that n is not larger than the range of the
228    // underlying RNG
229    if (n>rng_->max()) {
230      std::stringstream ss("DiscreteUniform::operator(unsigned long): ");
231      ss << n << " is too large for RNG " << rng_->name();
232      throw utility::GSL_error(ss.str());
233    }
234    return gsl_rng_uniform_int(rng_->rng(),n);
235  }
236
237
238  Poisson::Poisson(const double m)
239    : m_(m)
240  {
241  }
242
243  unsigned long Poisson::operator()(void) const
244  {
245    return gsl_ran_poisson(rng_->rng(), m_);
246  }
247
248
249  unsigned long Poisson::operator()(const double m) const
250  {
251    return gsl_ran_poisson(rng_->rng(), m);
252  }
253
254  // --------------------- Continuous distribtuions ---------------------
255
256  Continuous::Continuous(void)
257    : rng_(RNG::instance())
258  {
259  }
260
261
262  Continuous::~Continuous(void)
263  {
264  }
265
266
267  void Continuous::seed(unsigned long s) const
268  {
269    rng_->seed(s);
270  }
271
272
273  ContinuousGeneral::ContinuousGeneral(const statistics::Histogram& hist)
274    : discrete_(DiscreteGeneral(hist)), hist_(hist)
275  {
276  }
277
278
279  double ContinuousGeneral::operator()(void) const
280  {
281    return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing();
282  }
283
284  double ContinuousUniform::operator()(void) const
285  {
286    return gsl_rng_uniform(rng_->rng());
287  }
288
289
290  Exponential::Exponential(const double m)
291    : m_(m)
292  {
293  }
294
295
296  double Exponential::operator()(void) const
297  {
298    return gsl_ran_exponential(rng_->rng(), m_);
299  }
300
301
302  double Exponential::operator()(const double m) const
303  {
304    return gsl_ran_exponential(rng_->rng(), m);
305  }
306
307
308  Gaussian::Gaussian(const double s, const double m)
309    : m_(m), s_(s)
310  {
311  }
312
313
314  double Gaussian::operator()(void) const
315  {
316    return gsl_ran_gaussian(rng_->rng(), s_)+m_;
317  }
318
319
320  double Gaussian::operator()(const double s) const
321  {
322    return gsl_ran_gaussian(rng_->rng(), s);
323  }
324
325
326  double Gaussian::operator()(const double s, const double m) const
327  {
328    return gsl_ran_gaussian(rng_->rng(), s)+m;
329  }
330
331}}} // of namespace random, yat, and theplu
Note: See TracBrowser for help on using the repository browser.