mas01mc@292: /* mas01mc@292: A C-program for MT19937, with initialization improved 2002/1/26. mas01mc@292: Coded by Takuji Nishimura and Makoto Matsumoto. mas01mc@292: mas01mc@292: Before using, initialize the state by using init_genrand(seed) mas01mc@292: or init_by_array(init_key, key_length). mas01mc@292: mas01mc@292: Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, mas01mc@292: All rights reserved. mas01mc@292: Copyright (C) 2005, Mutsuo Saito, mas01mc@292: All rights reserved. mas01mc@292: mas01mc@292: Redistribution and use in source and binary forms, with or without mas01mc@292: modification, are permitted provided that the following conditions mas01mc@292: are met: mas01mc@292: mas01mc@292: 1. Redistributions of source code must retain the above copyright mas01mc@292: notice, this list of conditions and the following disclaimer. mas01mc@292: mas01mc@292: 2. Redistributions in binary form must reproduce the above copyright mas01mc@292: notice, this list of conditions and the following disclaimer in the mas01mc@292: documentation and/or other materials provided with the distribution. mas01mc@292: mas01mc@292: 3. The names of its contributors may not be used to endorse or promote mas01mc@292: products derived from this software without specific prior written mas01mc@292: permission. mas01mc@292: mas01mc@292: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS mas01mc@292: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT mas01mc@292: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR mas01mc@292: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR mas01mc@292: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, mas01mc@292: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, mas01mc@292: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR mas01mc@292: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF mas01mc@292: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING mas01mc@292: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS mas01mc@292: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. mas01mc@292: mas01mc@292: mas01mc@292: Any feedback is very welcome. mas01mc@292: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html mas01mc@292: email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) mas01mc@292: */ mas01mc@292: mas01mc@292: #include mas01mc@292: #include "mt19937ar.h" mas01mc@292: mas01mc@292: /* Period parameters */ mas01mc@292: #define N 624 mas01mc@292: #define M 397 mas01mc@292: #define MATRIX_A 0x9908b0dfUL /* constant vector a */ mas01mc@292: #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ mas01mc@292: #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ mas01mc@292: mas01mc@292: static unsigned long mt[N]; /* the array for the state vector */ mas01mc@292: static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ mas01mc@292: mas01mc@292: /* initializes mt[N] with a seed */ mas01mc@292: void init_genrand(unsigned long s) mas01mc@292: { mas01mc@292: mt[0]= s & 0xffffffffUL; mas01mc@292: for (mti=1; mti> 30)) + mti); mas01mc@292: /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ mas01mc@292: /* In the previous versions, MSBs of the seed affect */ mas01mc@292: /* only MSBs of the array mt[]. */ mas01mc@292: /* 2002/01/09 modified by Makoto Matsumoto */ mas01mc@292: mt[mti] &= 0xffffffffUL; mas01mc@292: /* for >32 bit machines */ mas01mc@292: } mas01mc@292: } mas01mc@292: mas01mc@292: /* initialize by an array with array-length */ mas01mc@292: /* init_key is the array for initializing keys */ mas01mc@292: /* key_length is its length */ mas01mc@292: /* slight change for C++, 2004/2/26 */ mas01mc@292: void init_by_array(unsigned long init_key[], int key_length) mas01mc@292: { mas01mc@292: int i, j, k; mas01mc@292: init_genrand(19650218UL); mas01mc@292: i=1; j=0; mas01mc@292: k = (N>key_length ? N : key_length); mas01mc@292: for (; k; k--) { mas01mc@292: mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) mas01mc@292: + init_key[j] + j; /* non linear */ mas01mc@292: mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ mas01mc@292: i++; j++; mas01mc@292: if (i>=N) { mt[0] = mt[N-1]; i=1; } mas01mc@292: if (j>=key_length) j=0; mas01mc@292: } mas01mc@292: for (k=N-1; k; k--) { mas01mc@292: mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) mas01mc@292: - i; /* non linear */ mas01mc@292: mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ mas01mc@292: i++; mas01mc@292: if (i>=N) { mt[0] = mt[N-1]; i=1; } mas01mc@292: } mas01mc@292: mas01mc@292: mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ mas01mc@292: } mas01mc@292: mas01mc@292: /* generates a random number on [0,0xffffffff]-interval */ mas01mc@292: unsigned long genrand_int32(void) mas01mc@292: { mas01mc@292: unsigned long y; mas01mc@292: static unsigned long mag01[2]={0x0UL, MATRIX_A}; mas01mc@292: /* mag01[x] = x * MATRIX_A for x=0,1 */ mas01mc@292: mas01mc@292: if (mti >= N) { /* generate N words at one time */ mas01mc@292: int kk; mas01mc@292: mas01mc@292: if (mti == N+1) /* if init_genrand() has not been called, */ mas01mc@292: init_genrand(5489UL); /* a default initial seed is used */ mas01mc@292: mas01mc@292: for (kk=0;kk> 1) ^ mag01[y & 0x1UL]; mas01mc@292: } mas01mc@292: for (;kk> 1) ^ mag01[y & 0x1UL]; mas01mc@292: } mas01mc@292: y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); mas01mc@292: mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; mas01mc@292: mas01mc@292: mti = 0; mas01mc@292: } mas01mc@292: mas01mc@292: y = mt[mti++]; mas01mc@292: mas01mc@292: /* Tempering */ mas01mc@292: y ^= (y >> 11); mas01mc@292: y ^= (y << 7) & 0x9d2c5680UL; mas01mc@292: y ^= (y << 15) & 0xefc60000UL; mas01mc@292: y ^= (y >> 18); mas01mc@292: mas01mc@292: return y; mas01mc@292: } mas01mc@292: mas01mc@292: /* generates a random number on [0,0x7fffffff]-interval */ mas01mc@292: long genrand_int31(void) mas01mc@292: { mas01mc@292: return (long)(genrand_int32()>>1); mas01mc@292: } mas01mc@292: mas01mc@292: /* generates a random number on [0,1]-real-interval */ mas01mc@292: double genrand_real1(void) mas01mc@292: { mas01mc@292: return genrand_int32()*(1.0/4294967295.0); mas01mc@292: /* divided by 2^32-1 */ mas01mc@292: } mas01mc@292: mas01mc@292: /* generates a random number on [0,1)-real-interval */ mas01mc@292: double genrand_real2(void) mas01mc@292: { mas01mc@292: return genrand_int32()*(1.0/4294967296.0); mas01mc@292: /* divided by 2^32 */ mas01mc@292: } mas01mc@292: mas01mc@292: /* generates a random number on (0,1)-real-interval */ mas01mc@292: double genrand_real3(void) mas01mc@292: { mas01mc@292: return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0); mas01mc@292: /* divided by 2^32 */ mas01mc@292: } mas01mc@292: mas01mc@292: /* generates a random number on [0,1) with 53-bit resolution*/ mas01mc@292: double genrand_res53(void) mas01mc@292: { mas01mc@292: unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; mas01mc@292: return(a*67108864.0+b)*(1.0/9007199254740992.0); mas01mc@292: } mas01mc@292: /* These real versions are due to Isaku Wada, 2002/01/09 added */