From 12540653f859a0a667d23b1f13f679e81ced2459 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Tue, 2 Jun 2009 07:48:18 +0000 Subject: [PATCH] fix random class --- .../Benchmarks/SingleTM/common/Random.java | 74 ++++++++++--------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/Robust/src/Benchmarks/SingleTM/common/Random.java b/Robust/src/Benchmarks/SingleTM/common/Random.java index 9cb065a6..ab46bf13 100644 --- a/Robust/src/Benchmarks/SingleTM/common/Random.java +++ b/Robust/src/Benchmarks/SingleTM/common/Random.java @@ -1,23 +1,28 @@ public class Random { - long[] mt; + int[] mt; int mti; - long RANDOM_DEFAULT_SEED; + int RANDOM_DEFAULT_SEED; /* period parameter */ int N; int M; - long MATRIX_A; - long UPPER_MASK; - long LOWER_MASK; + int MATRIX_A; + int UPPER_MASK; + int LOWER_MASK; + int[] mag01; public Random() { - RANDOM_DEFAULT_SEED = 0L; + RANDOM_DEFAULT_SEED = 0; N = 624; M = 397; - mt = new long[N]; + mt = new int[N]; mti = N; - MATRIX_A = 0x9908b0dfL; /* constant vector a */ - UPPER_MASK = 0x80000000L; /* most significant w-r bits */ - LOWER_MASK = 0x7fffffffL; /* least significant r bits */ + MATRIX_A = 0x9908b0df; /* constant vector a */ + UPPER_MASK = 0x80000000; /* most significant w-r bits */ + LOWER_MASK = 0x7fffffff; /* least significant r bits */ + mag01 = new int[2]; + mag01[0] = 0x0; + mag01[1] = MATRIX_A; + } public void random_alloc() { @@ -25,54 +30,51 @@ public class Random { } /* initializes mt[N] with a seed */ - public void init_genrand(long s) { - int mti; - mt[0]= s & 0xFFFFFFFFL; - for (mti=1; mti> 30)) + mti); + public void init_genrand(int s) { + mt[0]= s & 0xFFFFFFFF; + for (int mti=1; mti> 30)) + mti); /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ /* In the previous versions, MSBs of the seed affect */ /* only MSBs of the array mt[]. */ /* 2002/01/09 modified by Makoto Matsumoto */ - mt[mti] &= 0xFFFFFFFFL; + mt[mti] &= 0xFFFFFFFF; /* for >32 bit machines */ } this.mti=mti; } - public void random_seed(long seed) { + public void random_seed(int seed) { init_genrand(seed); } - public long random_generate() { + public int random_generate() { return genrand_int32(); } - public long genrand_int32() { - long y; - long[] mag01= new long[2]; - mag01[0] = 0x0L; - mag01[1] = MATRIX_A; + public int genrand_int32() { + int y; int mti = this.mti; /* mag01[x] = x * MATRIX_A for x=0,1 */ - if (mti >= N) { /* generate N words at one time */ + if (mti >= 624) { /* generate N words at one time */ int kk; + int[] mt = this.mt; - if (mti == N+1) /* if init_genrand() has not been called, */ - init_genrand(5489L); /* a default initial seed is used */ + if (mti == 624+1) /* if init_genrand() has not been called, */ + init_genrand(5489); /* a default initial seed is used */ - for (kk=0;kk> 1) ^ mag01[(int)(y & 0x1L)]; + for (kk=0;kk<(624-397);kk++) { + y = (mt[kk]&0x80000000)|(mt[kk+1]&0x7fffffff); + mt[kk] = mt[kk+397] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0:0x9908b0df); } - for (;kk> 1) ^ mag01[(int)(y & 0x1L)]; + for (;kk<(624-1);kk++) { + y = (mt[kk]&0x80000000)|(mt[kk+1]&0x7fffffff); + mt[kk] = mt[kk+(397-624)] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0:0x9908b0df); } - y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); - mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)]; + y = (mt[624-1]&0x80000000)|(mt[0]&0x7fffffff); + mt[624-1] = mt[397-1] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0:0x9908b0df); mti = 0; } @@ -81,8 +83,8 @@ public class Random { /* Tempering */ y ^= (y >> 11); - y ^= (y << 7) & 0x9d2c5680L; - y ^= (y << 15) & 0xefc60000L; + y ^= (y << 7) & 0x9d2c5680; + y ^= (y << 15) & 0xefc60000; y ^= (y >> 18); this.mti = mti; -- 2.34.1