/** * Reinitialize the generator as if just built with the given int array seed. * * <p>The state of the generator is exactly the same as a new generator built with the same seed. * * @param seed the initial seed (32 bits integers array), if null the seed of the generator will * be related to the current time */ public void setSeed(int[] seed) { if (seed == null) { setSeed(System.currentTimeMillis()); return; } setSeed(19650218); int i = 1; int j = 0; for (int k = Math.max(N, seed.length); k != 0; k--) { long l0 = (mt[i] & 0x7fffffffl) | ((mt[i] < 0) ? 0x80000000l : 0x0l); long l1 = (mt[i - 1] & 0x7fffffffl) | ((mt[i - 1] < 0) ? 0x80000000l : 0x0l); long l = (l0 ^ ((l1 ^ (l1 >> 30)) * 1664525l)) + seed[j] + j; // non linear mt[i] = (int) (l & 0xffffffffl); i++; j++; if (i >= N) { mt[0] = mt[N - 1]; i = 1; } if (j >= seed.length) { j = 0; } } for (int k = N - 1; k != 0; k--) { long l0 = (mt[i] & 0x7fffffffl) | ((mt[i] < 0) ? 0x80000000l : 0x0l); long l1 = (mt[i - 1] & 0x7fffffffl) | ((mt[i - 1] < 0) ? 0x80000000l : 0x0l); long l = (l0 ^ ((l1 ^ (l1 >> 30)) * 1566083941l)) - i; // non linear mt[i] = (int) (l & 0xffffffffL); i++; if (i >= N) { mt[0] = mt[N - 1]; i = 1; } } mt[0] = 0x80000000; // MSB is 1; assuring non-zero initial array }
/** * Creates a new random number generator. * * <p>The instance is initialized using the current time as the seed. */ public MersenneTwister() { mt = new int[N]; setSeed(System.currentTimeMillis()); }
/** * Reinitialize the generator as if just built with the given long seed. * * <p>The state of the generator is exactly the same as a new generator built with the same seed. * * @param seed the initial seed (64 bits integer) */ public void setSeed(long seed) { setSeed(new int[] {(int) (seed >>> 32), (int) (seed & 0xffffffffl)}); }
/** * Creates a new random number generator using a single long seed. * * @param seed the initial seed (64 bits integer) */ public MersenneTwister(long seed) { mt = new int[N]; setSeed(seed); }