1 package org.jahia.utils.keygenerator; 2 3 4 import java.io.IOException ; 5 import java.io.ObjectInputStream ; 6 import java.io.ObjectOutputStream ; 7 import java.io.Serializable ; 8 9 94 95 public class MersenneTwister extends java.util.Random implements Serializable 96 { 97 private static final int N = 624; 99 private static final int M = 397; 100 private static final int MATRIX_A = 0x9908b0df; private static final int UPPER_MASK = 0x80000000; private static final int LOWER_MASK = 0x7fffffff; 104 105 107 private static final int TEMPERING_MASK_B = 0x9d2c5680; 108 private static final int TEMPERING_MASK_C = 0xefc60000; 109 110 115 private int mt[]; private int mti; private int mag01[]; 118 119 private static final long GOOD_SEED = 4357; 121 122 123 126 public MersenneTwister() 127 { 128 super(GOOD_SEED); 129 setSeed(GOOD_SEED); 130 } 131 132 138 public MersenneTwister(long seed) 139 { 140 super(seed); 141 setSeed(seed); 142 } 143 144 158 159 synchronized public void setSeed(long seed) 160 { 161 162 167 super.setSeed(seed); 168 169 mt = new int[N]; 170 171 176 179 mt[0]= ((int)seed); 181 for (mti = 1; mti < N; mti++) 182 mt[mti] = (69069 * mt[mti-1]); 184 mag01 = new int[2]; 186 mag01[0] = 0x0; 187 mag01[1] = MATRIX_A; 188 } 189 190 193 synchronized protected int next(int bits) 194 { 195 int y; 196 197 if (mti >= N) { 199 int kk; 200 201 for (kk = 0; kk < N - M; kk++) 202 { 203 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 204 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 205 } 206 for (; kk < N-1; kk++) 207 { 208 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 209 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 210 } 211 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 212 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 213 214 mti = 0; 215 } 216 217 y = mt[mti++]; 218 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 223 return y >>> (32 - bits); } 225 226 227 229 230 private synchronized void writeObject(ObjectOutputStream out) 231 throws IOException 232 { 233 out.defaultWriteObject(); 235 } 236 237 private synchronized void readObject (ObjectInputStream in) 238 throws IOException , ClassNotFoundException 239 { 240 in.defaultReadObject(); 242 } 243 244 246 public boolean nextBoolean() {return next(1) != 0;} 247 248 250 251 public int nextInt(int n) { 252 if (n<=0) 253 throw new IllegalArgumentException ("n must be positive"); 254 255 if ((n & -n) == n) return (int)((n * (long)next(31)) >> 31); 257 258 int bits, val; 259 do { 260 bits = next(31); 261 val = bits % n; 262 } while(bits - val + (n-1) < 0); 263 return val; 264 } 265 266 268 public double nextDouble() 269 { 270 return (((long)next(26) << 27) + next(27)) 271 / (double)(1L << 53); 272 } 273 274 276 277 278 public float nextFloat() 279 { 280 return next(24) / ((float)(1 << 24)); 281 } 282 283 286 287 public void nextBytes(byte[] bytes) 288 { 289 for (int x=0;x<bytes.length;x++) 290 bytes[x] = (byte)next(8); 291 } 292 293 294 295 public char nextChar() 296 { 297 return (char)(next(16)); 299 } 300 301 302 303 public short nextShort() 304 305 { 306 return (short)(next(16)); 307 } 308 309 310 311 public byte nextByte() 312 { 313 return (byte)(next(8)); 314 } 315 316 317 318 321 public static void main(String args[]) 322 { 323 int j; 324 325 MersenneTwister r; 326 327 329 340 341 342 345 359 360 361 363 379 380 381 385 481 } 482 483 } 484 | Popular Tags |