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

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

Fixes #8 and #179, addresses #2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1// $Id: random.cc 738 2007-01-07 23:08:39Z jari $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "random.h"
25#include "yat/statistics/Histogram.h"
26#include "yat/utility/Exception.h"
27
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: unknown generator");
42    if (!(rng_=gsl_rng_alloc(gsl_rng_default)))
43      throw utility::GSL_error("RNG: failed to allocate memory");
44  }
45
46
47  RNG::~RNG(void)
48  {
49    gsl_rng_free(rng_);
50    rng_=NULL;
51    delete instance_;
52  }
53
54 
55  u_long RNG::max(void) const
56  {
57    return gsl_rng_max(rng_);
58  }
59
60
61  u_long RNG::min(void) const
62  {
63    return gsl_rng_min(rng_);
64  }
65
66
67  std::string RNG::name(void) const
68  {
69    return gsl_rng_name(rng_);
70  }
71
72
73  const gsl_rng* RNG::rng(void) const
74  {
75    return rng_;
76  }
77
78
79  void RNG::seed(u_long s) const
80  {
81    gsl_rng_set(rng_,s);
82  }
83
84
85  u_long RNG::seed_from_devurandom(void)
86  {
87    u_char ulongsize=sizeof(u_long);
88    char* buffer=new char[ulongsize];
89    std::ifstream is("/dev/urandom");
90    is.read(buffer,ulongsize);
91    is.close();
92    u_long s=0;
93    for (u_int i=0; i<ulongsize; i++) {
94      u_char ch=buffer[i];
95      s+=ch<<((ulongsize-1-i)*8);
96    }
97    seed(s);
98    return s;
99  }
100
101
102  int RNG::set_state(const RNG_state& state)
103  {
104    return gsl_rng_memcpy(rng_, state.rng());
105  }
106
107  // --------------------- RNG_state ----------------------------------
108
109  RNG_state::RNG_state(const RNG* rng)
110  {
111    if (!(rng_ = gsl_rng_clone(rng->rng())))
112      throw utility::GSL_error("RNG_state: failed to allocate memory");
113  }
114 
115
116  RNG_state::~RNG_state(void)
117  {
118    gsl_rng_free(rng_);
119    rng_=NULL;
120  }
121
122  const gsl_rng* RNG_state::rng(void) const
123  {
124    return rng_;
125  }
126
127  // --------------------- Discrete distribtuions ---------------------
128
129  Discrete::Discrete(void)
130    : rng_(RNG::instance())
131  {
132  }
133
134 
135  Discrete::~Discrete(void)
136  {
137  }
138
139
140  void Discrete::seed(u_long s) const
141  {
142    rng_->seed(s);
143  }
144
145
146  DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist)
147  {
148    double* p = new double[ hist.nof_bins() ];
149    for (size_t i=0; i<hist.nof_bins(); i++) 
150      p[ i ] = hist[i];
151    gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p );
152    delete p;
153    if (!gen_)
154      throw utility::GSL_error("DiscreteGeneral: failed to setup generator.");
155  }
156
157
158  DiscreteGeneral::~DiscreteGeneral(void)
159  {
160    if (gen_)
161      gsl_ran_discrete_free( gen_ );
162    gen_ = NULL;
163  }
164
165
166  u_long DiscreteGeneral::operator()(void) const
167  {
168    return gsl_ran_discrete(rng_->rng(), gen_);
169  }
170
171
172  DiscreteUniform::DiscreteUniform(u_long n)
173    : range_(n)
174  {
175    if (range_>rng_->max()) {
176      std::stringstream ss("DiscreteUniform: ");
177      ss << n << " is too large for RNG " << rng_->name();
178      throw utility::GSL_error(ss.str());
179    }
180  }
181
182
183  u_long DiscreteUniform::operator()(void) const
184  {
185    return (range_ ?
186            gsl_rng_uniform_int(rng_->rng(), range_) : gsl_rng_get(rng_->rng()));
187  }
188
189
190  u_long DiscreteUniform::operator()(u_long n) const
191  {
192    // making sure that n is not larger than the range of the
193    // underlying RNG
194    if (n>rng_->max()) {
195      std::stringstream ss("DiscreteUniform::op(): ");
196      ss << n << " is too large for RNG " << rng_->name();
197      throw utility::GSL_error(ss.str());
198    }
199    return gsl_rng_uniform_int(rng_->rng(),n);
200  }
201
202
203  Poisson::Poisson(const double m)
204    : m_(m)
205  {
206  }
207
208  u_long Poisson::operator()(void) const
209  {
210    return gsl_ran_poisson(rng_->rng(), m_);
211  }
212
213
214  u_long Poisson::operator()(const double m) const
215  {
216    return gsl_ran_poisson(rng_->rng(), m);
217  }
218
219  // --------------------- Continuous distribtuions ---------------------
220
221  Continuous::Continuous(void)
222    : rng_(RNG::instance())
223  {
224  }
225
226
227  Continuous::~Continuous(void)
228  {
229  }
230
231
232  void Continuous::seed(u_long s) const
233  {
234    rng_->seed(s);
235  }
236
237
238  ContinuousGeneral::ContinuousGeneral(const statistics::Histogram& hist)
239    : discrete_(DiscreteGeneral(hist)), hist_(hist)
240  {
241  }
242
243
244  double ContinuousGeneral::operator()(void) const
245  {
246    return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing();
247  }
248
249  double ContinuousUniform::operator()(void) const
250  {
251    return gsl_rng_uniform(rng_->rng());
252  }
253
254
255  Exponential::Exponential(const double m)
256    : m_(m)
257  {
258  }
259
260
261  double Exponential::operator()(void) const
262  {
263    return gsl_ran_exponential(rng_->rng(), m_);
264  }
265
266
267  double Exponential::operator()(const double m) const
268  {
269    return gsl_ran_exponential(rng_->rng(), m);
270  }
271
272
273  Gaussian::Gaussian(const double s, const double m)
274    : m_(m), s_(s)
275  {
276  }
277
278
279  double Gaussian::operator()(void) const
280  {
281    return gsl_ran_gaussian(rng_->rng(), s_)+m_;
282  }
283
284
285  double Gaussian::operator()(const double s) const
286  {
287    return gsl_ran_gaussian(rng_->rng(), s);
288  }
289
290
291  double Gaussian::operator()(const double s, const double m) const
292  {
293    return gsl_ran_gaussian(rng_->rng(), s)+m;
294  }
295
296}}} // of namespace random, yat, and theplu
Note: See TracBrowser for help on using the repository browser.