source: branches/0.3.1/yat/random/random.cc @ 838

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

Refs #185.

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