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