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

Last change on this file since 1537 was 1487, checked in by Jari Häkkinen, 15 years ago

Addresses #436. GPL license copy reference should also be updated.

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