/*1A C-program for MT19937, with initialization improved 2002/1/26.2Coded by Takuji Nishimura and Makoto Matsumoto.34Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,5All rights reserved.67Redistribution and use in source and binary forms, with or without8modification, are permitted provided that the following conditions9are met:10111. Redistributions of source code must retain the above copyright12notice, this list of conditions and the following disclaimer.13142. Redistributions in binary form must reproduce the above copyright15notice, this list of conditions and the following disclaimer in the16documentation and/or other materials provided with the distribution.17183. The names of its contributors may not be used to endorse or promote19products derived from this software without specific prior written20permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS23"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT24LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR25A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR26CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.333435Any feedback is very welcome.36http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html37email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)38*/3940#include <assert.h>41#include <stdio.h>42#include <stdlib.h>43#include "mutator_aux.h"4445#define init_genrand prng_init46#define genrand_int32 prng_uint324748/* Period parameters */49#define N 62450#define M 39751#define MATRIX_A 0x9908b0dfUL /* constant vector a */52#define UPPER_MASK 0x80000000UL /* most significant w-r bits */53#define LOWER_MASK 0x7fffffffUL /* least significant r bits */5455int prng_up = 0;56static unsigned long mt[N]; /* the array for the state vector */57static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */5859/* initializes mt[N] with a seed */60void init_genrand(unsigned long s)61{62mt[0]= s & 0xffffffffUL;63for (mti=1; mti<N; mti++) {64mt[mti] =65(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) +66(unsigned long)mti);67/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */68/* In the previous versions, MSBs of the seed affect */69/* only MSBs of the array mt[]. */70/* 2002/01/09 modified by Makoto Matsumoto */71mt[mti] &= 0xffffffffUL;72/* for >32 bit machines */73}74prng_up = 1;75}7677/* generates a random number on [0,0xffffffff]-interval */78unsigned long genrand_int32(void)79{80unsigned long y;81static unsigned long mag01[2]={0x0UL, MATRIX_A};82/* mag01[x] = x * MATRIX_A for x=0,1 */8384if (mti >= N) { /* generate N words at one time */85int kk;8687assert(mti != N+1);8889for (kk=0;kk<N-M;kk++) {90y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);91mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];92}93for (;kk<N-1;kk++) {94y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);95mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];96}97y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);98mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];99100mti = 0;101}102103y = mt[mti++];104105/* Tempering */106y ^= (y >> 11);107y ^= (y << 7) & 0x9d2c5680UL;108y ^= (y << 15) & 0xefc60000UL;109y ^= (y >> 18);110111return y;112}113114115