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

Last change on this file since 2587 was 2587, checked in by Peter, 11 years ago

avoid memory leak

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1// $Id: random.cc 2587 2011-10-25 21:27:19Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009, 2011 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    delete[] buffer;
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    clone(*rng->rng());
120  }
121 
122
123  RNG_state::RNG_state(const RNG_state& state)
124  {
125    clone(*state.rng());
126  }
127 
128
129  RNG_state::~RNG_state(void)
130  {
131    gsl_rng_free(rng_);
132    rng_=NULL;
133  }
134
135  const gsl_rng* RNG_state::rng(void) const
136  {
137    return rng_;
138  }
139
140
141  void RNG_state::clone(const gsl_rng& rng)
142  {
143    assert(rng_!=&rng);
144    if (!(rng_ = gsl_rng_clone(&rng)))
145      throw utility::GSL_error("RNG_state::RNG_state failed to allocate memory");
146  }
147
148  RNG_state& RNG_state::operator=(const RNG_state& rhs)
149  {
150    if (this != &rhs) {
151      gsl_rng_free(rng_);
152      clone(*rhs.rng());
153    }
154    return *this;
155  }
156
157  // --------------------- Discrete distribtuions ---------------------
158
159  Discrete::Discrete(void)
160    : rng_(RNG::instance())
161  {
162  }
163
164 
165  Discrete::~Discrete(void)
166  {
167  }
168
169
170  void Discrete::seed(unsigned long s) const
171  {
172    rng_->seed(s);
173  }
174
175
176  unsigned long Discrete::seed_from_devurandom(void) 
177  { 
178    return rng_->seed_from_devurandom(); 
179  }
180
181
182  DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist)
183    : gen_(NULL)
184  {
185    p_.reserve(hist.nof_bins());
186    for (size_t i=0; i<hist.nof_bins(); i++) 
187      p_.push_back(hist[i]);
188    preproc();
189  }
190
191
192  DiscreteGeneral::DiscreteGeneral(const DiscreteGeneral& other)
193    : Discrete(other), gen_(NULL), p_(other.p_)
194  {
195    preproc();
196  }
197
198
199  DiscreteGeneral::~DiscreteGeneral(void)
200  {
201    free();
202  }
203
204
205  void DiscreteGeneral::free(void)
206  {
207    if (gen_)
208      gsl_ran_discrete_free( gen_ );
209    gen_ = NULL;
210  }
211
212
213  void DiscreteGeneral::preproc(void)
214  {
215    assert(!gen_);
216    assert(p_.size());
217    gen_ = gsl_ran_discrete_preproc( p_.size(), &p_.front() );
218    if (!gen_)
219      throw utility::GSL_error("DiscreteGeneral failed to setup generator.");
220  }
221
222
223  DiscreteGeneral& DiscreteGeneral::operator=(const DiscreteGeneral& rhs)
224  {
225    free();
226    p_ = rhs.p_;
227    preproc();
228    return *this;
229  }
230
231
232  unsigned long DiscreteGeneral::operator()(void) const
233  {
234    return gsl_ran_discrete(rng_->rng(), gen_);
235  }
236
237
238  DiscreteUniform::DiscreteUniform(unsigned long n)
239    : range_(n)
240  {
241    if (range_>rng_->max()) {
242      std::stringstream ss("DiscreteUniform::DiscreteUniform: ");
243      ss << n << " is too large for RNG " << rng_->name();
244      throw utility::GSL_error(ss.str());
245    }
246  }
247
248
249  unsigned long DiscreteUniform::operator()(void) const
250  {
251    return (range_ ?
252            gsl_rng_uniform_int(rng_->rng(), range_) : gsl_rng_get(rng_->rng()));
253  }
254
255
256  unsigned long DiscreteUniform::operator()(unsigned long n) const
257  {
258    // making sure that n is not larger than the range of the
259    // underlying RNG
260    if (n>rng_->max()) {
261      std::stringstream ss("DiscreteUniform::operator(unsigned long): ");
262      ss << n << " is too large for RNG " << rng_->name();
263      throw utility::GSL_error(ss.str());
264    }
265    return gsl_rng_uniform_int(rng_->rng(),n);
266  }
267
268
269  Poisson::Poisson(const double m)
270    : m_(m)
271  {
272  }
273
274  unsigned long Poisson::operator()(void) const
275  {
276    return gsl_ran_poisson(rng_->rng(), m_);
277  }
278
279
280  unsigned long Poisson::operator()(const double m) const
281  {
282    return gsl_ran_poisson(rng_->rng(), m);
283  }
284
285  // --------------------- Continuous distribtuions ---------------------
286
287  Continuous::Continuous(void)
288    : rng_(RNG::instance())
289  {
290  }
291
292
293  Continuous::~Continuous(void)
294  {
295  }
296
297
298  void Continuous::seed(unsigned long s) const
299  {
300    rng_->seed(s);
301  }
302
303
304  unsigned long Continuous::seed_from_devurandom(void)
305  {
306    return rng_->seed_from_devurandom();
307  }
308
309
310  ContinuousGeneral::ContinuousGeneral(const statistics::Histogram& hist)
311    : discrete_(DiscreteGeneral(hist)), hist_(hist)
312  {
313  }
314
315
316  double ContinuousGeneral::operator()(void) const
317  {
318    return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing();
319  }
320
321  double ContinuousUniform::operator()(void) const
322  {
323    return gsl_rng_uniform(rng_->rng());
324  }
325
326
327  Exponential::Exponential(const double m)
328    : m_(m)
329  {
330  }
331
332
333  double Exponential::operator()(void) const
334  {
335    return gsl_ran_exponential(rng_->rng(), m_);
336  }
337
338
339  double Exponential::operator()(const double m) const
340  {
341    return gsl_ran_exponential(rng_->rng(), m);
342  }
343
344
345  Gaussian::Gaussian(const double s, const double m)
346    : m_(m), s_(s)
347  {
348  }
349
350
351  double Gaussian::operator()(void) const
352  {
353    return gsl_ran_gaussian(rng_->rng(), s_)+m_;
354  }
355
356
357  double Gaussian::operator()(const double s) const
358  {
359    return gsl_ran_gaussian(rng_->rng(), s);
360  }
361
362
363  double Gaussian::operator()(const double s, const double m) const
364  {
365    return gsl_ran_gaussian(rng_->rng(), s)+m;
366  }
367
368}}} // of namespace random, yat, and theplu
Note: See TracBrowser for help on using the repository browser.