SST/macro
mersenne_twister.h
Go to the documentation of this file.
1 /*
2  * Mersenne twister adapted for SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  *
5  * The original code has the following license information:
6  *
7  **********************************
8  *
9  * A C-program for MT19937-64 (2004/9/29 version).
10  * Coded by Takuji Nishimura and Makoto Matsumoto.
11  *
12  * This is a 64-bit version of Mersenne Twister pseudorandom number
13  * generator.
14  *
15  * Before using, initialize the state by using init_genrand64(seed)
16  * or init_by_array64(init_key, key_length).
17  *
18  * Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  *
25  * 1. Redistributions of source code must retain the above copyright
26  * notice, this list of conditions and the following disclaimer.
27  *
28  * 2. Redistributions in binary form must reproduce the above copyright
29  * notice, this list of conditions and the following disclaimer in the
30  * documentation and/or other materials provided with the distribution.
31  *
32  * 3. The names of its contributors may not be used to endorse or promote
33  * products derived from this software without specific prior written
34  * permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
40  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
41  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  *
48  * References:
49  * T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
50  * ACM Transactions on Modeling and
51  * Computer Simulation 10. (2000) 348--357.
52  * M. Matsumoto and T. Nishimura,
53  * ``Mersenne Twister: a 623-dimensionally equidistributed
54  * uniform pseudorandom number generator''
55  * ACM Transactions on Modeling and
56  * Computer Simulation 8. (Jan. 1998) 3--30.
57  *
58  * Any feedback is very welcome.
59  * http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
60  * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
61  *
62  ***************************************
63  *
64  * Modest changes made to adapt the code for SST/macroscale:
65  * 1) Global (static) state eliminated -- state vector is now member data.
66  * 2) Code moved into namespace sstmac::RNG and put in a class derived from
67  * UniformInteger.
68  * 3) Some function signatures and types renamed to better match function
69  * names in SST/macro.
70 */
71 
72 #ifndef SSTMAC_COMMON_MERSENNETWISTER_H_INCLUDED
73 #define SSTMAC_COMMON_MERSENNETWISTER_H_INCLUDED
74 
75 #include <sstmac/common/rng.h>
76 #include <stdint.h>
77 
78 
79 
80 namespace RNG {
81 
82 /**
83  * Updated Mersenne twister.
84  *
85  * Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
86  * All rights reserved.
87  *
88  * Redistribution and use in source and binary forms, with or without
89  * modification, are permitted provided that the following conditions
90  * are met:
91  *
92  * 1. Redistributions of source code must retain the above copyright
93  * notice, this list of conditions and the following disclaimer.
94  *
95  * 2. Redistributions in binary form must reproduce the above copyright
96  * notice, this list of conditions and the following disclaimer in the
97  * documentation and/or other materials provided with the distribution.
98  *
99  * 3. The names of its contributors may not be used to endorse or promote
100  * products derived from this software without specific prior written
101  * permission.
102  *
103  * Modified to fit into the SST/macroscale code structure
104  * and to eliminate global (static) state.
105  */
107 {
108  /// State vector length defaults to 312 entries ('NN' in original code).
109  static const int state_vector_length_;
110  /// I believe this is a mask length, defaults to 156 ('MM' in original).
111  static const int mask_length_;
112  /// The array for the state vector. Of length 'state_vector_length_'.
113  std::vector<uint64_t> mt_;
114  /// The current position in the state vector ('mti' in original code).
115  size_t mti_;
116 
117  void reseed(uint64_t seed);
118 
119  /// Like normally for SST/macro types, constructor is private.
120  mersenne_twister(uint64_t seed);
121 
122  public:
123  virtual std::string
124  to_string() const {
125  return "mersennetwister";
126  }
127 
128  /// Create a new twister.
129  static mersenne_twister* construct(uint64_t seed);
130 
131  /// Cleanup.
132  virtual ~mersenne_twister();
133 
134  /// Get a value in the interval [0,numeric_limits<rngint_t>::max()]
135  virtual rngint_t value();
136 
137  /// Reseed the generator. This is an expensive operation.
138  virtual void vec_reseed(const std::vector<rngint_t> &seeds);
139 
140  /// The number of seeds employed by this generator.
141  /// The generator will accept any value up to state_vector_length_.
142  virtual int nseed() {
143  return state_vector_length_;
144  }
145 };
146 
147 } // end of namespace RNG.
148 
149 
150 
151 #endif
152 
virtual ~mersenne_twister()
Cleanup.
std::vector< uint64_t > mt_
The array for the state vector. Of length &#39;state_vector_length_&#39;.
This is a base class for random number generators that return an integer uniformly distributed in a r...
Definition: rng.h:26
static const int mask_length_
I believe this is a mask length, defaults to 156 (&#39;MM&#39; in original).
virtual std::string to_string() const
virtual int nseed()
The number of seeds employed by this generator.
static const int state_vector_length_
State vector length defaults to 312 entries (&#39;NN&#39; in original code).
uint32_t rngint_t
Definition: rng.h:22
virtual void vec_reseed(const std::vector< rngint_t > &seeds)
Reseed the generator. This is an expensive operation.
virtual rngint_t value()
Get a value in the interval [0,numeric_limits<rngint_t>::max()].
mersenne_twister(uint64_t seed)
Like normally for SST/macro types, constructor is private.
static mersenne_twister * construct(uint64_t seed)
Create a new twister.
size_t mti_
The current position in the state vector (&#39;mti&#39; in original code).
Updated Mersenne twister.