Skip to content

Instantly share code, notes, and snippets.

@odanado
Last active August 29, 2015 14:00
Show Gist options
  • Save odanado/11238158 to your computer and use it in GitHub Desktop.
Save odanado/11238158 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdint>
#include <cstdio>
class MT {
uint32_t mt_table[624];
uint32_t mt_count;
public:
MT(uint32_t s){
mt_table[0] = s;
for(int i=1; i<624; i++) {
mt_table[i] = ((mt_table[i-1] >> 30) ^ mt_table[i-1]) * 0x6c078965 + i;
}
mt_count = 0;
}
uint32_t operator()() {
const uint32_t b = (mt_count + 1) % 624;
const uint32_t c = (mt_count + 397) % 624;
const uint32_t k0 = (mt_table[mt_count] & 0x80000000UL) | (mt_table[b] & 0x7FFFFFFFUL);
mt_table[mt_count] = mt_table[c] ^ (k0 >> 1) ^ (0x9908B0DFUL * (k0 & 1));
uint32_t k = mt_table[mt_count];
mt_count = b;
k ^= (k >> 11);
k ^= (k << 7) & 0x9D2C5680UL;
k ^= (k << 15) & 0xEFC60000UL;
k ^= (k >> 18);
return k;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment