NEST  2.6.0,not_revisioned_source_dir@0
mt19937.h
Go to the documentation of this file.
1 #ifndef MT19937_H
2 #define MT19937_H
3 
4 /*
5  This is a modified version of (see below for details):
6 
7  A C-program for MT19937, with initialization improved 2002/1/26.
8  Coded by Takuji Nishimura and Makoto Matsumoto.
9 
10  Before using, initialize the state by using init_genrand(seed)
11  or init_by_array(init_key, key_length).
12 
13  Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
14  All rights reserved.
15  Copyright (C) 2005, Mutsuo Saito
16  All rights reserved.
17 
18  Redistribution and use in source and binary forms, with or without
19  modification, are permitted provided that the following conditions
20  are met:
21 
22  1. Redistributions of source code must retain the above copyright
23  notice, this list of conditions and the following disclaimer.
24 
25  2. Redistributions in binary form must reproduce the above copyright
26  notice, this list of conditions and the following disclaimer in the
27  documentation and/or other materials provided with the distribution.
28 
29  3. The names of its contributors may not be used to endorse or promote
30  products derived from this software without specific prior written
31  permission.
32 
33  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 
45 
46  Any feedback is very welcome.
47  http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
48  email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
49 */
50 
51 /*
52  Modifications to the originial mt19937.{h,c} code by Nishimura, Matsumoto and
53  Saito:
54  - Implemented as C++ class with local variables, to allow for multiple generators.
55  - Inclusion guard added.
56 
57  Hans Ekkehard Plesser, 2008-01-03
58 
59 */
60 
61 #include "randomgen.h"
62 #include <vector>
63 
64 namespace librandom
65 {
72  class MT19937 : public RandomGen {
73  public:
74 
76  explicit MT19937(unsigned long);
77 
78  ~MT19937() {};
79 
80  RngPtr clone(unsigned long s)
81  {
82  return RngPtr(new MT19937(s));
83  }
84 
85  private:
87  void seed_(unsigned long);
88 
90  double drand_();
91 
92  private:
93  // functions inherited from C-version of mt19937
94 
95  /* initializes mt[N] with a seed */
96  void init_genrand(unsigned long);
97 
98  /* generates a random number on [0,0xffffffff]-interval */
99  unsigned long genrand_int32();
100 
101  /* generates a random number on [0,1)-real-interval */
102  double genrand_real2();
103 
104  /* Period parameters */
105  static const unsigned int N;
106  static const unsigned int M;
107  static const unsigned long MATRIX_A; /* constant vector a */
108  static const unsigned long UPPER_MASK; /* most significant w-r bits */
109  static const unsigned long LOWER_MASK; /* least significant r bits */
110  static const double I2DFactor_;
111 
112  std::vector<unsigned long> mt; /* the array for the state vector */
113  int mti; /* mti==N+1 means mt[N] is not initialized */
114  };
115 }
116 
117 inline
119 {
120  return genrand_real2();
121 }
122 
123 inline
125 {
126  return I2DFactor_ * genrand_int32();
127  /* divided by 2^32 */
128 }
129 
130 
131 #endif
static const double I2DFactor_
int to double factor
Definition: mt19937.h:110
static const unsigned long LOWER_MASK
Definition: mt19937.h:109
static const unsigned long MATRIX_A
Definition: mt19937.h:107
Mersenne Twister MT19937.
Definition: mt19937.h:72
double drand_()
implements drawing a single [0,1) number for RandomGen
Definition: mt19937.h:118
static const unsigned int M
Definition: mt19937.h:106
std::vector< unsigned long > mt
Definition: mt19937.h:112
RngPtr clone(unsigned long s)
clone a random number generator of same type initialized with given seed
Definition: mt19937.h:80
static const unsigned int N
Definition: mt19937.h:105
double genrand_real2()
Definition: mt19937.h:124
lockPTR< RandomGen > RngPtr
Common lock-pointer type for RNG.
Definition: randomgen.h:208
unsigned long genrand_int32()
Definition: mt19937.cpp:94
int mti
Definition: mt19937.h:113
void init_genrand(unsigned long)
Definition: mt19937.cpp:79
Abstract base class for all random generator objects.
Definition: randomgen.h:238
~MT19937()
Definition: mt19937.h:78
MT19937(unsigned long)
Create generator with given seed.
Definition: mt19937.cpp:67
static const unsigned long UPPER_MASK
Definition: mt19937.h:108
void seed_(unsigned long)
implements seeding for RandomGen
Definition: mt19937.cpp:74