/** * Sets the seed of the MersenneTwister using an array of integers. Your array must have a * non-zero length. Only the first 624 integers in the array are used; if the array is shorter * than this then integers are repeatedly used in a wrap-around fashion. */ public synchronized void setSeed(final int[] array) { if (array.length == 0) throw new IllegalArgumentException("Array length must be greater than zero"); int i, j, k; setSeed(19650218); i = 1; j = 0; k = (N > array.length ? N : array.length); for (; k != 0; k--) { mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * 1664525)) + array[j] + j; /* non linear */ mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */ i++; j++; if (i >= N) { mt[0] = mt[N - 1]; i = 1; } if (j >= array.length) j = 0; } for (k = N - 1; k != 0; k--) { mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * 1566083941)) - i; /* non linear */ mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */ i++; if (i >= N) { mt[0] = mt[N - 1]; i = 1; } } mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */ }
/** * Constructor using an array of integers as seed. Your array must have a non-zero length. Only * the first 624 integers in the array are used; if the array is shorter than this then integers * are repeatedly used in a wrap-around fashion. */ public MersenneTwisterFast(final int[] array) { setSeed(array); }
/** * Constructor using a given seed. Though you pass this seed in as a long, it's best to make sure * it's actually an integer. */ public MersenneTwisterFast(final long seed) { setSeed(seed); }