NEST  2.6.0,not_revisioned_source_dir@0
randomgen.h
Go to the documentation of this file.
1 /*
2  * randomgen.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST 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
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef RANDOMGEN_H
24 #define RANDOMGEN_H
25 
165 /*
166  * Problems:
167  * Some problems remain to be solved:
168  * -- portability has to be confirmed (on compilers other
169  * than g++ and > 32 Bit architectures.
170  *
171  * History:
172  * (1) first version (<= V1.0) by Gewaltig
173  * (2) rewritten for C++ (V2.0) by Diesmann 6.9.93
174  * Note! The July 8,1993 Version contains an error
175  * in random3 and in random2. In this Version the
176  * correct lines are marked.
177  * (3) 16.9.93 class Normal is no longer a RNG but
178  * a deviation. This change was necessary to pre-
179  * vent side effects in cases where random numbers
180  * are used in several classes of one program.
181  * (4) Rewritten C++ base class and three more
182  * generators added. (Gewaltig)
183  * (5) Completely re-designed: GSL based, randomdict
184  * introduced (HEP)
185  * (6) GSL and standalone version now provide the
186  * knuthlfg originally introduced in (4)
187  * Diesmann 21.08.02
188  * (7) virtual function binomial added as interface
189  * for BinomialRandomDev to reach binomial function
190  * of gslrandomgen
191  * (8) Buffered drawing of RNG, all non-drand() methods
192  * now go via drand(); binomial removed, now imple-
193  * mented in BinomialRandomDev. (HEP, 28.06.04)
194  * (9) All code derived from the GSL removed (HEP, 09.01.08).
195  */
196 
197 #include <vector>
198 #include <cmath>
199 
200 #include "lockptr.h"
201 
206 namespace librandom {
207 
208  class RandomGen;
209 
216  typedef lockPTR<RandomGen> RngPtr;
217 
238  class RandomGen {
239 
240  private:
241  static const size_t DEFAULT_BUFFSIZE;
242 
243  public:
244 
252  RandomGen();
253 
254  // ensures proper clean up
255  virtual ~RandomGen() {};
256 
263  double drand(void);
264  double operator()(void);
265  double drandpos(void);
266  unsigned long ulrand(const unsigned long);
267 
268  void seed(const unsigned long);
269 
270  size_t get_buffsize(void) const;
271  void set_buffsize(const size_t);
272 
279  static RngPtr create_knuthlfg_rng(unsigned long);
280 
282  static const unsigned long DefaultSeed;
283 
285  virtual RngPtr clone(const unsigned long) = 0;
286 
287  protected:
288 
294  virtual void seed_(unsigned long) =0;
295  virtual double drand_() =0;
296 
297  private:
298 
299  void refill_();
300 
301  std::vector<double> buffer_;
302  std::vector<double>::const_iterator next_;
303  std::vector<double>::const_iterator end_;
304 
305  // prohibit copying of RNG
306  RandomGen(const RandomGen&);
307 
308  };
309 
315  {
316  public:
324  virtual RngPtr create(unsigned long) const =0;
325 
326  virtual ~GenericRNGFactory() {}
327  };
328 
334  template <typename Generator>
336  {
337  RngPtr create(unsigned long s) const { return RngPtr(new Generator(s)); }
338  };
339 
340  inline
341  double RandomGen::drand(void)
342  {
343  if ( next_ == end_ )
344  refill_();
345 
346  return *next_++;
347  }
348 
349  inline
351  {
352  return drand();
353  }
354 
355  inline
356  double RandomGen::drandpos(void)
357  {
358  double r;
359 
360  do {
361  r = drand();
362  } while ( r == 0.0 );
363 
364  return r;
365  }
366 
367  inline
368  unsigned long RandomGen::ulrand(const unsigned long n)
369  {
370  // no check for size of n required, since n is ulong
371  return static_cast<unsigned long>(std::floor(n * drand()));
372  }
373 
374  inline
375  size_t RandomGen::get_buffsize(void) const
376  {
377  return buffer_.size();
378  }
379 
380 }
381 
382 #endif // RANDOMGEN_H
383 
384 
size_t get_buffsize(void) const
returns buffer size
Definition: randomgen.h:375
void refill_()
refill buffer
Definition: randomgen.cpp:67
virtual double drand_()=0
drawing interface
static const size_t DEFAULT_BUFFSIZE
Definition: randomgen.h:241
double drandpos(void)
draw from (0, 1)
Definition: randomgen.h:356
void set_buffsize(const size_t)
set buffer size
Definition: randomgen.cpp:43
void seed(const unsigned long)
set random seed to a new value
Definition: randomgen.cpp:58
RngPtr create(unsigned long s) const
Create generator with given seed.
Definition: randomgen.h:337
double drand(void)
The following functions implement the user interface of the RandomGen class, including buffer managem...
Definition: randomgen.h:341
double operator()(void)
draw from [0, 1)
Definition: randomgen.h:350
static const unsigned long DefaultSeed
Default value for seeding generators in places where no seed is supplied.
Definition: randomgen.h:282
lockPTR< RandomGen > RngPtr
Common lock-pointer type for RNG.
Definition: randomgen.h:208
unsigned long ulrand(const unsigned long)
draw from [0, n-1]
Definition: randomgen.h:368
Factory class for random generators.
Definition: randomgen.h:314
virtual RngPtr clone(const unsigned long)=0
clone a random number generator of same type initialized with given seed
Concrete template for factories for built-in (non GSL) random generators.
Definition: randomgen.h:335
std::vector< double >::const_iterator end_
== buffer_.end()
Definition: randomgen.h:303
RandomGen()
Definition: randomgen.cpp:29
Abstract base class for all random generator objects.
Definition: randomgen.h:238
virtual ~GenericRNGFactory()
Definition: randomgen.h:326
std::vector< double > buffer_
random number buffer
Definition: randomgen.h:301
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
virtual void seed_(unsigned long)=0
The following functions provide the interface to the actual random generator.
std::vector< double >::const_iterator next_
next number to return
Definition: randomgen.h:302
virtual ~RandomGen()
Definition: randomgen.h:255
virtual RngPtr create(unsigned long) const =0
Create generator with given seed.
static RngPtr create_knuthlfg_rng(unsigned long)
Create built-in Knuth Lagged Fibonacci random generator.
Definition: randomgen.cpp:77