47 #include "mt19937ar.h" 52 #define MATRIX_A 0x9908b0dfUL 53 #define UPPER_MASK 0x80000000UL 54 #define LOWER_MASK 0x7fffffffUL 56 static unsigned long mt[N];
60 void init_genrand(
unsigned long s)
62 mt[0]= s & 0xffffffffUL;
63 for (mti=1; mti<N; mti++) {
65 (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
70 mt[mti] &= 0xffffffffUL;
79 void init_by_array(
unsigned long init_key[],
int key_length)
82 init_genrand(19650218UL);
84 k = (N>key_length ? N : key_length);
86 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
88 mt[i] &= 0xffffffffUL;
90 if (i>=N) { mt[0] = mt[N-1]; i=1; }
91 if (j>=key_length) j=0;
94 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
96 mt[i] &= 0xffffffffUL;
98 if (i>=N) { mt[0] = mt[N-1]; i=1; }
101 mt[0] = 0x80000000UL;
105 unsigned long genrand_int32(
void)
108 static unsigned long mag01[2]={0x0UL, MATRIX_A};
115 init_genrand(5489UL);
117 for (kk=0;kk<N-M;kk++) {
118 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
119 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
122 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
123 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
125 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
126 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
135 y ^= (y << 7) & 0x9d2c5680UL;
136 y ^= (y << 15) & 0xefc60000UL;
143 long genrand_int31(
void)
145 return (
long)(genrand_int32()>>1);
149 double genrand_real1(
void)
151 return genrand_int32()*(1.0/4294967295.0);
156 double genrand_real2(
void)
158 return genrand_int32()*(1.0/4294967296.0);
163 double genrand_real3(
void)
165 return (((
double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
170 double genrand_res53(
void)
172 unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
173 return(a*67108864.0+b)*(1.0/9007199254740992.0);