NEST  2.6.0,not_revisioned_source_dir@0
knuthlfg.h
Go to the documentation of this file.
1 /*
2  * knuthlfg.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 KNUTHLFG_H
24 #define KNUTHLFG_H
25 
26 /*
27  * Built-in implementation of Knuth's LFG generator.
28  * This code is a C++ adaptation of the code published by Knuth on his
29  * website, http://www-cs-faculty.stanford.edu/~knuth/programs/rng.c,
30  * retrieved 8 Jan 2008. See also Knuth's header comment below.
31  */
32 
33 /* Header comment by D E Knuth ------------------------------------------ */
34 
35 /* This program by D E Knuth is in the public domain and freely copyable.
36  * It is explained in Seminumerical Algorithms, 3rd edition, Section 3.6
37  * (or in the errata to the 2nd edition --- see
38  * http://www-cs-faculty.stanford.edu/~knuth/taocp.html
39  * in the changes to Volume 2 on pages 171 and following). */
40 
41 /* N.B. The MODIFICATIONS introduced in the 9th printing (2002) are
42  included here; there's no backwards compatibility with the original. */
43 
44 /* This version also adopts Brendan McKay's suggestion to
45  accommodate naive users who forget to call ran_start(seed). */
46 
47 /* If you find any bugs, please report them immediately to
48  * taocp@cs.stanford.edu
49  * (and you will be rewarded if the bug is genuine). Thanks! */
50 
51 /************ see the book for explanations and caveats! *******************/
52 /************ in particular, you need two's complement arithmetic **********/
53 
54 /* End of Header comment by D E Knuth ----------------------------------- */
55 
56 #include "randomgen.h"
57 #include <vector>
58 
59 namespace librandom {
60 
66  class KnuthLFG : public RandomGen {
67  public:
68 
70  explicit KnuthLFG(unsigned long);
71 
72  ~KnuthLFG() {};
73 
74  RngPtr clone(unsigned long s)
75  {
76  return RngPtr(new KnuthLFG(s));
77  }
78 
79  private:
81  void seed_(unsigned long);
82 
84  double drand_();
85 
86  private:
87  static const long KK_;
88  static const long LL_;
89  static const long MM_;
90  static const long TT_;
91  static const long QUALITY_;
92  static const double I2DFactor_;
93 
94  static long mod_diff_(long, long);
95  static bool is_odd_(long);
96 
97  std::vector<long> ran_x_;
98  std::vector<long> ran_buffer_;
99  const std::vector<long>::const_iterator end_;
100  std::vector<long>::const_iterator next_;
101 
107  void ran_array_(std::vector<long>& rbuff);
108  void ran_start_(long seed);
109  long ran_draw_();
110 
117  void self_test_();
118  };
119 
120  inline
121  void KnuthLFG::seed_(unsigned long seed)
122  {
123  ran_start_(seed);
124  }
125 
126 
127  inline
129  {
130  return I2DFactor_ * ran_draw_();
131  }
132 
133 
134  inline
135  long KnuthLFG::mod_diff_(long x, long y)
136  {
137  // modulo computation assumes two's complement
138  return ( x - y ) & ( MM_ - 1 );
139  }
140 
141  inline
142  bool KnuthLFG::is_odd_(long x)
143  {
144  return x & 1;
145  }
146 
147  inline long KnuthLFG::ran_draw_()
148  {
149  if ( next_ == end_ )
150  {
151  ran_array_(ran_buffer_); // refill
152  next_ = ran_buffer_.begin();
153  }
154 
155  return *next_++; // return next and increment
156  }
157 
158 } // namespace librandom
159 
160 #endif
static const double I2DFactor_
int to double factor
Definition: knuthlfg.h:92
void ran_start_(long seed)
initializes buffer
Definition: knuthlfg.cpp:84
static const long KK_
the long lag
Definition: knuthlfg.h:87
void self_test_()
Perform minimal self-test given by Knuth.
Definition: knuthlfg.cpp:114
const unsigned long seed
Definition: randomtest.cpp:52
static const long LL_
the short lag
Definition: knuthlfg.h:88
static const long MM_
the modulus
Definition: knuthlfg.h:89
void seed(const unsigned long)
set random seed to a new value
Definition: randomgen.cpp:58
std::vector< long > ran_x_
the generator state
Definition: knuthlfg.h:97
void seed_(unsigned long)
implements seeding for RandomGen
Definition: knuthlfg.h:121
static bool is_odd_(long)
Definition: knuthlfg.h:142
RngPtr clone(unsigned long s)
clone a random number generator of same type initialized with given seed
Definition: knuthlfg.h:74
static const long QUALITY_
number of RNGs to fill for each cycle
Definition: knuthlfg.h:91
const Name y("y")
Definition: topology_names.h:52
lockPTR< RandomGen > RngPtr
Common lock-pointer type for RNG.
Definition: randomgen.h:208
const std::vector< long >::const_iterator end_
marker past last to deliver
Definition: knuthlfg.h:99
Built-in implementation of Knuth's Lagged Fibonacci generator.
Definition: knuthlfg.h:66
static long mod_diff_(long, long)
subtraction module MM
Definition: knuthlfg.h:135
const Name x("x")
current scaling factor of the synaptic weight [0...1] (Tsodyks2_connection)
Definition: nest_names.h:356
void ran_array_(std::vector< long > &rbuff)
Generates numbers, refilling buffer.
Definition: knuthlfg.cpp:72
Abstract base class for all random generator objects.
Definition: randomgen.h:238
long ran_draw_()
deliver integer random number from ran_buffer_
Definition: knuthlfg.h:147
~KnuthLFG()
Definition: knuthlfg.h:72
std::vector< long >::const_iterator next_
next number to deliver
Definition: knuthlfg.h:100
static const long TT_
guaranteed separation between streams
Definition: knuthlfg.h:90
double drand_()
implements drawing a single [0,1) number for RandomGen
Definition: knuthlfg.h:128
KnuthLFG(unsigned long)
Create generator with given seed.
Definition: knuthlfg.cpp:62
std::vector< long > ran_buffer_
generated numbers, 0..KK-1 are shipped
Definition: knuthlfg.h:98