1 #ifndef MTTOOLBOX_MERSENNETWISTER_HPP
2 #define MTTOOLBOX_MERSENNETWISTER_HPP
60 mt =
new uint32_t[LARGE_N];
76 mt =
new uint32_t[LARGE_N];
92 mt =
new uint32_t[LARGE_N];
110 mt =
new uint32_t[LARGE_N];
140 for (mti = 1; mti < N; mti++) {
142 (UINT32_C(1812433253)
143 * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
158 void seed(
const std::string& value) {
159 seed_array<char>(value.c_str(),
static_cast<int>(value.size()));
175 void seed(
const uint32_t *value,
int key_length) {
176 seed_array<uint32_t>(value, key_length);
194 template<
class T>
void seed_array(
const T *value,
int key_length) {
196 seed(UINT32_C(19650218));
199 if (N > key_length) {
205 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30))
206 * UINT32_C(1664525)))
207 +
static_cast<uint32_t
>(value[j]) + j;
218 for (k=N-1; k; k--) {
219 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30))
220 * UINT32_C(1566083941)))
229 mt[0] = UINT32_C(0x80000000);
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)};
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];
273 mti = (mti + 1) & LARGE_MASK;
295 enum {LARGE_N = 1024, N = 624, M = 397};
298 uint32_t temper(uint32_t y) {
300 y ^= (y << 7) & UINT32_C(0x9d2c5680);
301 y ^= (y << 15) & UINT32_C(0xefc60000);
307 #endif // MTTOOLBOX_MERSENNETWISTER_HPP