MTToolBox  0.2.10
MersenneTwister64.hpp
[詳解]
1 #ifndef MTTOOLBOX_MERSENNETWISTER64_HPP
2 #define MTTOOLBOX_MERSENNETWISTER64_HPP
3 
32 #include <stdint.h>
33 #include <inttypes.h>
34 #include <string>
36 #if defined(DEBUG)
37 #include <iostream>
38 #include <iomanip>
39 #endif
40 
41 namespace MTToolBox {
53  class MersenneTwister64 : public AbstractGenerator<uint64_t> {
54  public:
65  mt = new uint64_t[N];
66  seed(UINT64_C(19650218));
67  }
68 
80  MersenneTwister64(uint64_t value) {
81  mt = new uint64_t[N];
82  seed(value);
83  }
84 
96  MersenneTwister64(const std::string& value) {
97  mt = new uint64_t[N];
98  seed(value);
99  }
100 
114  MersenneTwister64(const uint64_t *value, int size) {
115  mt = new uint64_t[N];
116  seed(value, size);
117  }
118 
129  delete[] mt;
130  }
131 
143  void seed(uint64_t value) {
144  mt[0] = value;
145  for (mti = 1; mti < N; mti++) {
146  mt[mti] = mti
147  + UINT64_C(6364136223846793005)
148  * (mt[mti-1] ^ (mt[mti-1] >> 62));
149  }
150  mti = 0;
151  }
152 
164  void seed(const std::string& value) {
165  seed_array<char>(value.c_str(), static_cast<int>(value.size()));
166  }
167 
181  void seed(const uint64_t *value, int key_length) {
182  seed_array<uint64_t>(value, key_length);
183  }
184 
200  template<class T> void seed_array(const T *init_key,
201  int key_length) {
202  int i, j;
203  uint64_t k;
204  seed(UINT64_C(19650218));
205  i = 1;
206  j = 0;
207  k = (N > key_length ? N : key_length);
208  for (; k; k--) {
209  mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 62))
210  * UINT64_C(3935559000370003845)))
211  + init_key[j] + (uint64_t)j; /* non linear */
212  i++;
213  j++;
214  if (i>=N) {
215  mt[0] = mt[N-1];
216  i = 1;
217  }
218  if (j >= key_length) {
219  j=0;
220  }
221  }
222  for (k=N-1; k; k--) {
223  mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 62))
224  * UINT64_C(2862933555777941757)))
225  - (uint64_t)i; /* non linear */
226  i++;
227  if (i>=N) {
228  mt[0] = mt[N-1];
229  i=1;
230  }
231  }
232  /* MSB is 1; assuring non-zero initial array */
233  mt[0] = UINT64_C(1) << 63;
234  mti = 0;
235  }
236 
248  uint64_t generate() {
249  return next();
250  }
251 
263  uint64_t next() {
264  const uint64_t UPPER_MASK = UINT64_C(0xFFFFFFFF80000000);
265  const uint64_t LOWER_MASK = UINT64_C(0x000000007FFFFFFF);
266  const uint64_t mag01[2] = {0x0UL, UINT64_C(0xB5026F5AA96619E9)};
267  uint64_t x;
268 
269  x = (mt[mti] & UPPER_MASK) | (mt[(mti + 1) % N] & LOWER_MASK);
270  mt[mti] = mt[(mti + M) % N] ^ (x >> 1)
271  ^ mag01[(int)(x & UINT64_C(1))];
272  x = mt[mti];
273  mti = (mti + 1) % N;
274  return temper(x);
275  }
276 
288  int bitSize() const {
289  return 19937;
290  }
291  private:
292  enum {N = 312, M = 156};
293  uint64_t *mt; /* the array for the state vector */
294  int mti;
295  uint64_t temper(uint64_t y) {
296  y ^= (y >> 29) & UINT64_C(0x5555555555555555);
297  y ^= (y << 17) & UINT64_C(0x71D67FFFEDA60000);
298  y ^= (y << 37) & UINT64_C(0xFFF7EEE000000000);
299  y ^= (y >> 43);
300  return y;
301  }
302  };
303 }
304 #endif // MTTOOLBOX_MERSENNETWISTER64_HPP
疑似乱数生成器
Definition: AbstractGenerator.hpp:48
void seed(uint64_t value)
64bit整数による初期化
Definition: MersenneTwister64.hpp:143
void seed_array(const T *init_key, int key_length)
T 型配列による初期化
Definition: MersenneTwister64.hpp:200
GF(2)線形疑似乱数生成器の抽象クラス
MersenneTwister64(uint64_t value)
コンストラクタ
Definition: MersenneTwister64.hpp:80
MersenneTwister64(const std::string &value)
コンストラクタ
Definition: MersenneTwister64.hpp:96
uint64_t next()
疑似乱数を生成する
Definition: MersenneTwister64.hpp:263
MersenneTwister64()
コンストラクタ
Definition: MersenneTwister64.hpp:64
int bitSize() const
状態空間のビットサイズである 19937 を返す。
Definition: MersenneTwister64.hpp:288
64 bit Mersenne Twister 疑似乱数生成器
Definition: MersenneTwister64.hpp:53
void seed(const uint64_t *value, int key_length)
符号なし整数配列による初期化
Definition: MersenneTwister64.hpp:181
~MersenneTwister64()
デストラクタ
Definition: MersenneTwister64.hpp:128
MersenneTwister64(const uint64_t *value, int size)
コンストラクタ
Definition: MersenneTwister64.hpp:114
uint64_t generate()
疑似乱数を生成する
Definition: MersenneTwister64.hpp:248
void seed(const std::string &value)
string文字列による初期化
Definition: MersenneTwister64.hpp:164
MTToolBox の名前空間