1 1.1 christos /* 2 1.1 christos A C-program for MT19937, with initialization improved 2002/1/26. 3 1.1 christos Coded by Takuji Nishimura and Makoto Matsumoto. 4 1.1 christos 5 1.1 christos Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, 6 1.1 christos All rights reserved. 7 1.1 christos 8 1.1 christos Redistribution and use in source and binary forms, with or without 9 1.1 christos modification, are permitted provided that the following conditions 10 1.1 christos are met: 11 1.1 christos 12 1.1 christos 1. Redistributions of source code must retain the above copyright 13 1.1 christos notice, this list of conditions and the following disclaimer. 14 1.1 christos 15 1.1 christos 2. Redistributions in binary form must reproduce the above copyright 16 1.1 christos notice, this list of conditions and the following disclaimer in the 17 1.1 christos documentation and/or other materials provided with the distribution. 18 1.1 christos 19 1.1 christos 3. The names of its contributors may not be used to endorse or promote 20 1.1 christos products derived from this software without specific prior written 21 1.1 christos permission. 22 1.1 christos 23 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 27 1.1 christos CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 1.1 christos EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 1.1 christos PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 1.1 christos PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 1.1 christos LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 1.1 christos NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 1.1 christos SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 1.1 christos 35 1.1 christos 36 1.1 christos Any feedback is very welcome. 37 1.1 christos http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html 38 1.1 christos email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) 39 1.1 christos */ 40 1.1 christos 41 1.1 christos #include <assert.h> 42 1.1 christos #include <stdio.h> 43 1.1 christos #include <stdlib.h> 44 1.1 christos #include "mutator_aux.h" 45 1.1 christos 46 1.1 christos #define init_genrand prng_init 47 1.1 christos #define genrand_int32 prng_uint32 48 1.1 christos 49 1.1 christos /* Period parameters */ 50 1.1 christos #define N 624 51 1.1 christos #define M 397 52 1.1 christos #define MATRIX_A 0x9908b0dfUL /* constant vector a */ 53 1.1 christos #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ 54 1.1 christos #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ 55 1.1 christos 56 1.1 christos int prng_up = 0; 57 1.1 christos static unsigned long mt[N]; /* the array for the state vector */ 58 1.1 christos static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ 59 1.1 christos 60 1.1 christos /* initializes mt[N] with a seed */ 61 1.1 christos void init_genrand(unsigned long s) 62 1.1 christos { 63 1.1 christos mt[0]= s & 0xffffffffUL; 64 1.1 christos for (mti=1; mti<N; mti++) { 65 1.1 christos mt[mti] = 66 1.1.1.2 christos (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + 67 1.1.1.2 christos (unsigned long)mti); 68 1.1 christos /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 69 1.1 christos /* In the previous versions, MSBs of the seed affect */ 70 1.1 christos /* only MSBs of the array mt[]. */ 71 1.1 christos /* 2002/01/09 modified by Makoto Matsumoto */ 72 1.1 christos mt[mti] &= 0xffffffffUL; 73 1.1 christos /* for >32 bit machines */ 74 1.1 christos } 75 1.1 christos prng_up = 1; 76 1.1 christos } 77 1.1 christos 78 1.1 christos /* generates a random number on [0,0xffffffff]-interval */ 79 1.1 christos unsigned long genrand_int32(void) 80 1.1 christos { 81 1.1 christos unsigned long y; 82 1.1 christos static unsigned long mag01[2]={0x0UL, MATRIX_A}; 83 1.1 christos /* mag01[x] = x * MATRIX_A for x=0,1 */ 84 1.1 christos 85 1.1 christos if (mti >= N) { /* generate N words at one time */ 86 1.1 christos int kk; 87 1.1 christos 88 1.1 christos assert(mti != N+1); 89 1.1 christos 90 1.1 christos for (kk=0;kk<N-M;kk++) { 91 1.1 christos y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 92 1.1 christos mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL]; 93 1.1 christos } 94 1.1 christos for (;kk<N-1;kk++) { 95 1.1 christos y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 96 1.1 christos mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; 97 1.1 christos } 98 1.1 christos y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); 99 1.1 christos mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; 100 1.1 christos 101 1.1 christos mti = 0; 102 1.1 christos } 103 1.1 christos 104 1.1 christos y = mt[mti++]; 105 1.1 christos 106 1.1 christos /* Tempering */ 107 1.1 christos y ^= (y >> 11); 108 1.1 christos y ^= (y << 7) & 0x9d2c5680UL; 109 1.1 christos y ^= (y << 15) & 0xefc60000UL; 110 1.1 christos y ^= (y >> 18); 111 1.1 christos 112 1.1 christos return y; 113 1.1 christos } 114