MTToolBox  0.2.10
MersenneTwister.hpp
Go to the documentation of this file.
1 #ifndef MTTOOLBOX_MERSENNETWISTER_HPP
2 #define MTTOOLBOX_MERSENNETWISTER_HPP
3 
25 #include <stdint.h>
26 #include <inttypes.h>
27 #include <string>
29 
30 namespace MTToolBox {
48  class MersenneTwister : public AbstractGenerator<uint32_t> {
49  public:
60  mt = new uint32_t[LARGE_N];
61  seed(5489);
62  }
63 
75  MersenneTwister(uint32_t value) {
76  mt = new uint32_t[LARGE_N];
77  seed(value);
78  }
79 
91  MersenneTwister(const std::string& value) {
92  mt = new uint32_t[LARGE_N];
93  seed(value);
94  }
95 
109  MersenneTwister(const uint32_t *value, int size) {
110  mt = new uint32_t[LARGE_N];
111  seed(value, size);
112  }
113 
124  delete[] mt;
125  }
126 
138  void seed(uint32_t value) {
139  mt[0] = value;
140  for (mti = 1; mti < N; mti++) {
141  mt[mti] =
142  (UINT32_C(1812433253)
143  * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
144  }
145  }
146 
158  void seed(const std::string& value) {
159  seed_array<char>(value.c_str(), static_cast<int>(value.size()));
160  }
161 
175  void seed(const uint32_t *value, int key_length) {
176  seed_array<uint32_t>(value, key_length);
177  }
178 
194  template<class T> void seed_array(const T *value, int key_length) {
195  int i, j, k;
196  seed(UINT32_C(19650218));
197  i = 1;
198  j = 0;
199  if (N > key_length) {
200  k = N;
201  } else {
202  k = key_length;
203  }
204  for (; k > 0; k--) {
205  mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30))
206  * UINT32_C(1664525)))
207  + static_cast<uint32_t>(value[j]) + j; /* non linear */
208  i++;
209  j++;
210  if (i>=N) {
211  mt[0] = mt[N-1];
212  i=1;
213  }
214  if (j>=key_length) {
215  j=0;
216  }
217  }
218  for (k=N-1; k; k--) {
219  mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30))
220  * UINT32_C(1566083941)))
221  - i; /* non linear */
222  i++;
223  if (i>=N) {
224  mt[0] = mt[N-1];
225  i=1;
226  }
227  }
228  /* MSB is 1; assuring non-zero initial array */
229  mt[0] = UINT32_C(0x80000000);
230  mti = N;
231  }
232 
244  uint32_t generate() {
245  return next();
246  }
247 
259  uint32_t next() {
260  using namespace std;
261 
262  const uint32_t LARGE_MASK = UINT32_C(0x3ff);
263  const uint32_t UPPER_MASK = UINT32_C(0x80000000);
264  const uint32_t LOWER_MASK = UINT32_C(0x7fffffff);
265  const uint32_t mag01[2] = {0x0UL, UINT32_C(0x9908b0df)};
266  uint32_t y;
267 
268  y = (mt[(LARGE_N - N + mti) & LARGE_MASK] & UPPER_MASK)
269  | (mt[(LARGE_N - N + mti + 1) & LARGE_MASK] & LOWER_MASK);
270  mt[mti] = mt[(LARGE_N - N + M + mti) & LARGE_MASK]
271  ^ (y >> 1) ^ mag01[y & 0x1UL];
272  y = temper(mt[mti]);
273  mti = (mti + 1) & LARGE_MASK;
274  return y;
275  }
276 
291  int bitSize() const {
292  return 19937;
293  }
294  private:
295  enum {LARGE_N = 1024, N = 624, M = 397};
296  uint32_t *mt; /* the array for the state vector */
297  int mti;
298  uint32_t temper(uint32_t y) {
299  y ^= (y >> 11);
300  y ^= (y << 7) & UINT32_C(0x9d2c5680);
301  y ^= (y << 15) & UINT32_C(0xefc60000);
302  y ^= (y >> 18);
303  return y;
304  }
305  };
306 }
307 #endif // MTTOOLBOX_MERSENNETWISTER_HPP
void seed_array(const T *value, int key_length)
Initialization by an array of T type.
Definition: MersenneTwister.hpp:194
pseudo random number generator.
Definition: AbstractGenerator.hpp:48
uint32_t generate()
Generates pseudo random number.
Definition: MersenneTwister.hpp:244
MersenneTwister()
Constructor without arguments.
Definition: MersenneTwister.hpp:59
Abstract class of GF(2)-linear pseudo random number generators.
void seed(uint32_t value)
Initialization by 32-bit integer.
Definition: MersenneTwister.hpp:138
Mersenne Twister pseudo random number generator.
Definition: MersenneTwister.hpp:48
~MersenneTwister()
Destructor.
Definition: MersenneTwister.hpp:123
int bitSize() const
Returns 19937, which is size of internal state.
Definition: MersenneTwister.hpp:291
MersenneTwister(uint32_t value)
Constructor with a integer seed.
Definition: MersenneTwister.hpp:75
MersenneTwister(const uint32_t *value, int size)
Constructor with an array of integer.
Definition: MersenneTwister.hpp:109
uint32_t next()
Generates pseudo random number.
Definition: MersenneTwister.hpp:259
void seed(const std::string &value)
Initialization by a string.
Definition: MersenneTwister.hpp:158
void seed(const uint32_t *value, int key_length)
Initialization by an array of unsigned integers.
Definition: MersenneTwister.hpp:175
MersenneTwister(const std::string &value)
Constructor with a string seed.
Definition: MersenneTwister.hpp:91
name space for MTToolBox