try to work on memory usage...have large test case running now
[IRC.git] / Robust / src / Benchmarks / SingleTM / Bayes / Random.java
1 public class Random {
2   long[] mt; 
3   int mti;
4   int RANDOM_DEFAULT_SEED;
5   /* period parameter */
6
7
8   public Random() {
9     RANDOM_DEFAULT_SEED = 0;
10     mt = new long[624];
11   }
12
13   public void random_alloc() {
14     init_genrand(this.RANDOM_DEFAULT_SEED);
15   }
16
17   /* initializes mt[N] with a seed */
18   public void init_genrand(int s) {
19     mt[0]= ((long)s) & 0xFFFFFFFFL;
20     for (int mti=1; mti<624; mti++) {
21       mt[mti] = (1812433253L * (mt[mti-1] ^ (mt[mti-1] >> 30)) + ((long)mti));
22       /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
23       /* In the previous versions, MSBs of the seed affect   */
24       /* only MSBs of the array mt[].                        */
25       /* 2002/01/09 modified by Makoto Matsumoto             */
26       mt[mti] &= 0xFFFFFFFFL;
27       /* for >32 bit machines */
28     }
29     this.mti=624;
30   }
31
32   public void random_seed(int seed) {
33     init_genrand(seed);
34   }
35
36   public long random_generate() {
37     long x= genrand_int32()&0xFFFFFFFFL;
38     return x;
39   }
40
41   public long posrandom_generate() {
42     long r=genrand_int32();
43     if (r>0)
44       return r;
45     else 
46       return -r;
47   }
48
49   public long genrand_int32() {
50     long y;
51     int mti = this.mti;
52     long[] mt = this.mt;
53
54     if (mti >= 624) { /* generate N words at one time */
55       int kk;
56
57       if (mti == 624+1) {  /* if init_genrand() has not been called, */
58         init_genrand(5489); /* a default initial seed is used */
59         mti=this.mti;
60       }
61       for (kk=0;kk<(624-397);kk++) {
62         y = (mt[kk]&0x80000000L)|(mt[kk+1]&0x7fffffffL);
63         mt[kk] = mt[kk+397] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
64       }
65       for (;kk<(624-1);kk++) {
66         y = (mt[kk]&0x80000000L)|(mt[kk+1]&0x7fffffffL);
67         mt[kk] = mt[kk+(397-624)] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
68       }
69       y = (mt[624-1]&0x80000000L)|(mt[0]&0x7fffffffL);
70       mt[624-1] = mt[397-1] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
71
72       mti = 0;
73     }
74
75     y = mt[mti++];
76
77     /* Tempering */
78     y ^= (y >> 11);
79     y ^= (y << 7) & 0x9d2c5680L;
80     y ^= (y << 15) & 0xefc60000L;
81     y ^= (y >> 18);
82
83     this.mti = mti;
84
85     return y;
86   }
87 }