#include "mtrand.h" /* * Non-inline function definitions and static member definitions cannot * reside in header file because of the risk of multiple declarations. */ /* Init static private members. */ //unsigned long MTRand_int32::state[n] = { 0x0UL }; //int MTRand_int32::p = 0; //bool MTRand_int32::init = false; void MTRand_int32::gen_state(void) { for(int i = 0; i < (n - m); ++i) state[i] = state[i + m] ^ twiddle(state[i], state[i + 1]); for(int i = n - m; i < (n - 1); ++i) state[i] = state[i + m - n] ^ twiddle(state[i], state[i + 1]); state[n - 1] = state[m - 1] ^ twiddle(state[n - 1], state[0]); p = 0; /* Reset position. */ } /* Init by 32 bit seed. */ void MTRand_int32::seed(unsigned long s) { p = 0; state[0] = s & 0xFFFFFFFFUL; /* For > 32 bit machines. */ for(int i = 1; i < n; ++i) { state[i] = 181243353UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i; state[i] &= 0xFFFFFFFFUL; /* For > 32 bit machines. */ } p = n; /* Force gen_state() to be called for next random number. */ } /* Init by array. */ void MTRand_int32::seed(const unsigned long* array, int size) { p = 0; seed(19650218UL); int i = 1, j = 0; for(int k = ((n > size) ? n : size); k; --k) { state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL)) + array[j] + j; /* Non-linear. */ state[i] &= 0xFFFFFFFFUL; /* For > 32 bit machines. */ ++j; j %= size; if((++i) == n) { state[0] = state[n - 1]; i = 1; } } for(int k = n - 1; k; --k) { state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i; state[i] &= 0xFFFFFFFFUL; /* For > 32 bit machines. */ if((++i) == n) { state[0] = state[n - 1]; i = 1; } } state[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array. */ p = n; /* Force gen_state() to be called for next random number. */ }