0a1,2 > package nedragtna.random; > 28,29d29 < package net.goui.util; < 35,59c35,66 < *
< * A Java implementation of the MT19937 (Mersenne Twister) pseudo < * random number generator algorithm based upon the original C code < * by Makoto Matsumoto and Takuji Nishimura (see < * < * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html for < * more information. < *
< * As a subclass of java.util.Random this class provides a single < * canonical method next() for generating bits in the pseudo random < * number sequence. Anyone using this class should invoke the public < * inherited methods (nextInt(), nextFloat etc.) to obtain values as < * normal. This class should provide a drop-in replacement for the < * standard implementation of java.util.Random with the additional < * advantage of having a far longer period and the ability to use a < * far larger seed value. < *
< * This is not a cryptographically strong source of randomness < * and should not be used for cryptographic systems or in any < * other situation where true random numbers are required. < *
< *
< *
< * This software is licensed under the CC-GNU LGPL.
< *
---
> *
> * A Java implementation of the MT19937 (Mersenne Twister) pseudo random > * number generator algorithm based upon the original C code by Makoto > * Matsumoto and Takuji Nishimura (see * href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html"> > * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html for more > * information. > *
> * As a subclass of java.util.Random this class provides a single > * canonical method next() for generating bits in the pseudo random > * number sequence. Anyone using this class should invoke the public > * inherited methods (nextInt(), nextFloat etc.) to obtain values as > * normal. This class should provide a drop-in replacement for the > * standard implementation of java.util.Random with the additional > * advantage of having a far longer period and the ability to use a far > * larger seed value. > *
> * This is not a cryptographically strong source of randomness > * and should not be used for cryptographic systems or in any > * other situation where true random numbers are required. > *
> * * href="http://creativecommons.org/licenses/LGPL/2.1/"> * alt="CC-GNU LGPL" border="0"
> * src="http://creativecommons.org/images/public/cc-LGPL-a.png" />
> * This software is licensed under the * href="http://creativecommons.org/licenses/LGPL/2.1/">CC-GNU LGPL.
> *
> *
> *
---
> * -->
87,90c88,91
< * Auto-generated serial version UID. Note that MTRandom does NOT
< * support serialisation of its internal state and it may even be
< * necessary to implement read/write methods to re-seed it properly.
< * This is only here to make Eclipse shut up about it being missing.
---
> * Auto-generated serial version UID. Note that MTRandom does NOT support
> * serialisation of its internal state and it may even be necessary to
> * implement read/write methods to re-seed it properly. This is only here to
> * make Eclipse shut up about it being missing.
104,106c105,107
< private final static int MAGIC_MASK1 = 0x9d2c5680;
< private final static int MAGIC_MASK2 = 0xefc60000;
< private final static int MAGIC_SEED = 19650218;
---
> private final static int MAGIC_MASK1 = 0x9d2c5680;
> private final static int MAGIC_MASK2 = 0xefc60000;
> private final static int MAGIC_SEED = 19650218;
118,123c119,130
< * The default constructor for an instance of MTRandom. This invokes
< * the no-argument constructor for java.util.Random which will result
< * in the class being initialised with a seed value obtained by calling
< * System.currentTimeMillis().
< */
< public MTRandom() { }
---
> * The default constructor for an instance of MTRandom.
> * Since the no-argument constructor of java.util.Random
> * does not seem to call setSeed anymore (since JDK7),
> * we need to do it manually in this constructor.
> * For legacy purposes, the seed remains initialized by
> * a call to System.currentTimeMillis().
> * @author Jonathan Passerat-Palmbach
> *
> */
> public MTRandom() {
> this.setSeed(System.currentTimeMillis());
> }
128,129c135,136
< * exactly replicating the case where the seed value had not been set
< * prior to calling genrand_int32.
---
> * exactly replicating the case where the seed value had not been set prior
> * to calling genrand_int32.
132,136c139,143
< * seeded with the same default value as was used in the original C
< * code. Furthermore the setSeed() method, which must take a 64 bit
< * long value, will be limited to using only the lower 32 bits of the
< * seed to facilitate seamless migration of existing C code into Java
< * where identical behaviour is required.
---
> * seeded with the same default value as was used in the original C code.
> * Furthermore the setSeed() method, which must take a 64 bit long value,
> * will be limited to using only the lower 32 bits of the seed to facilitate
> * seamless migration of existing C code into Java where identical behaviour
> * is required.
138,140c145,147
< * Whilst useful for ensuring backwards compatibility, it is advised
< * that this feature not be used unless specifically required, due to
< * the reduction in strength of the seed value.
---
> * Whilst useful for ensuring backwards compatibility, it is advised that
> * this feature not be used unless specifically required, due to the
> * reduction in strength of the seed value.
142,143c149,150
< * @param compatible Compatibility flag for replicating original
< * behaviour.
---
> * @param compatible
> * Compatibility flag for replicating original behaviour.
148c155
< setSeed(compat?DEFAULT_SEED:System.currentTimeMillis());
---
> setSeed(compat ? DEFAULT_SEED : System.currentTimeMillis());
152,154c159,161
< * This version of the constructor simply initialises the class with
< * the given 64 bit seed value. For a better random number sequence
< * this seed value should contain as much entropy as possible.
---
> * This version of the constructor simply initialises the class with the
> * given 64 bit seed value. For a better random number sequence this seed
> * value should contain as much entropy as possible.
156c163,168
< * @param seed The seed value with which to initialise this class.
---
> * This constructor was modified due to be compliant to the JDK7's implementation
> * of java.util.Random as explained in MTRandom()
> * @param seed
> * The seed value with which to initialise this class.
> * @see MTRandom()
> * @author Jonathan Passerat-Palmbach
159c171,172
< super(seed);
---
> super(seed);
> this.setSeed(seed);
163,165c176,177
< * This version of the constructor initialises the class with the
< * given byte array. All the data will be used to initialise this
< * instance.
---
> * This version of the constructor initialises the class with the given byte
> * array. All the data will be used to initialise this instance.
167,169c179,184
< * @param buf The non-empty byte array of seed information.
< * @throws NullPointerException if the buffer is null.
< * @throws IllegalArgumentException if the buffer has zero length.
---
> * @param buf
> * The non-empty byte array of seed information.
> * @throws NullPointerException
> * if the buffer is null.
> * @throws IllegalArgumentException
> * if the buffer has zero length.
177,183c192,200
< * This version of the constructor initialises the class with the
< * given integer array. All the data will be used to initialise
< * this instance.
< *
< * @param buf The non-empty integer array of seed information.
< * @throws NullPointerException if the buffer is null.
< * @throws IllegalArgumentException if the buffer has zero length.
---
> * This version of the constructor initialises the class with the given
> * integer array. All the data will be used to initialise this instance.
> *
> * @param buf
> * The non-empty integer array of seed information.
> * @throws NullPointerException
> * if the buffer is null.
> * @throws IllegalArgumentException
> * if the buffer has zero length.
200c217,218
< if (mt == null) mt = new int[N];
---
> if (mt == null)
> mt = new int[N];
205c223
< mt[mti] = (MAGIC_FACTOR1 * (mt[mti-1] ^ (mt[mti-1] >>> 30)) + mti);
---
> mt[mti] = (MAGIC_FACTOR1 * (mt[mti - 1] ^ (mt[mti - 1] >>> 30)) + mti);
211,215c229,233
< * This method resets the state of this instance using the 64
< * bits of seed data provided. Note that if the same seed data
< * is passed to two different instances of MTRandom (both of
< * which share the same compatibility state) then the sequence
< * of numbers generated by both instances will be identical.
---
> * This method resets the state of this instance using the 64 bits of seed
> * data provided. Note that if the same seed data is passed to two different
> * instances of MTRandom (both of which share the same compatibility state)
> * then the sequence of numbers generated by both instances will be
> * identical.
217,220c235,238
< * If this instance was initialised in 'compatibility' mode then
< * this method will only use the lower 32 bits of any seed value
< * passed in and will match the behaviour of the original C code
< * exactly with respect to state initialisation.
---
> * If this instance was initialised in 'compatibility' mode then this method
> * will only use the lower 32 bits of any seed value passed in and will
> * match the behaviour of the original C code exactly with respect to state
> * initialisation.
222,223c240,242
< * @param seed The 64 bit value used to initialise the random
< * number generator state.
---
> * @param seed
> * The 64 bit value used to initialise the random number
> * generator state.
227c246
< setSeed((int)seed);
---
> setSeed((int) seed);
235c254,255
< if (ibuf == null) ibuf = new int[2];
---
> if (ibuf == null)
> ibuf = new int[2];
237,238c257,258
< ibuf[0] = (int)seed;
< ibuf[1] = (int)(seed >>> 32);
---
> ibuf[0] = (int) seed;
> ibuf[1] = (int) (seed >>> 32);
244,252c264,271
< * This method resets the state of this instance using the byte
< * array of seed data provided. Note that calling this method
< * is equivalent to calling "setSeed(pack(buf))" and in particular
< * will result in a new integer array being generated during the
< * call. If you wish to retain this seed data to allow the pseudo
< * random sequence to be restarted then it would be more efficient
< * to use the "pack()" method to convert it into an integer array
< * first and then use that to re-seed the instance. The behaviour
< * of the class will be the same in both cases but it will be more
---
> * This method resets the state of this instance using the byte array of
> * seed data provided. Note that calling this method is equivalent to
> * calling "setSeed(pack(buf))" and in particular will result in a new
> * integer array being generated during the call. If you wish to retain this
> * seed data to allow the pseudo random sequence to be restarted then it
> * would be more efficient to use the "pack()" method to convert it into an
> * integer array first and then use that to re-seed the instance. The
> * behaviour of the class will be the same in both cases but it will be more
254,257c273,279
< *
< * @param buf The non-empty byte array of seed information.
< * @throws NullPointerException if the buffer is null.
< * @throws IllegalArgumentException if the buffer has zero length.
---
> *
> * @param buf
> * The non-empty byte array of seed information.
> * @throws NullPointerException
> * if the buffer is null.
> * @throws IllegalArgumentException
> * if the buffer has zero length.
264,270c286,295
< * This method resets the state of this instance using the integer
< * array of seed data provided. This is the canonical way of
< * resetting the pseudo random number sequence.
< *
< * @param buf The non-empty integer array of seed information.
< * @throws NullPointerException if the buffer is null.
< * @throws IllegalArgumentException if the buffer has zero length.
---
> * This method resets the state of this instance using the integer array of
> * seed data provided. This is the canonical way of resetting the pseudo
> * random number sequence.
> *
> * @param buf
> * The non-empty integer array of seed information.
> * @throws NullPointerException
> * if the buffer is null.
> * @throws IllegalArgumentException
> * if the buffer has zero length.
274c299,300
< if (length == 0) throw new IllegalArgumentException("Seed buffer may not be empty");
---
> if (length == 0)
> throw new IllegalArgumentException("Seed buffer may not be empty");
279,282c305,314
< mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >>> 30)) * MAGIC_FACTOR2)) + buf[j] + j;
< i++; j++;
< if (i >= N) { mt[0] = mt[N-1]; i = 1; }
< if (j >= length) j = 0;
---
> mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * MAGIC_FACTOR2))
> + buf[j] + j;
> i++;
> j++;
> if (i >= N) {
> mt[0] = mt[N - 1];
> i = 1;
> }
> if (j >= length)
> j = 0;
284,285c316,318
< for (k = N-1; k > 0; k--) {
< mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >>> 30)) * MAGIC_FACTOR3)) - i;
---
> for (k = N - 1; k > 0; k--) {
> mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * MAGIC_FACTOR3))
> - i;
287c320,323
< if (i >= N) { mt[0] = mt[N-1]; i = 1; }
---
> if (i >= N) {
> mt[0] = mt[N - 1];
> i = 1;
> }
295,298c331,334
< * sequence from this class. If given a value of 32, this method
< * behaves identically to the genrand_int32 function in the original
< * C code and ensures that using the standard nextInt() function
< * (inherited from Random) we are able to replicate behaviour exactly.
---
> * sequence from this class. If given a value of 32, this method behaves
> * identically to the genrand_int32 function in the original C code and
> * ensures that using the standard nextInt() function (inherited from
> * Random) we are able to replicate behaviour exactly.
300,302c336,339
< * Note that where the number of bits requested is not equal to 32
< * then bits will simply be masked out from the top of the returned
< * integer value. That is to say that:
---
> * Note that where the number of bits requested is not equal to 32 then bits
> * will simply be masked out from the top of the returned integer value.
> * That is to say that:
> *
305c342,344
< * int foo = mt.nextInt(16) + (mt.nextInt(16) << 16);
---
> * int foo = mt.nextInt(16) + (mt.nextInt(16) << 16);
> *
> *
306a346
> *
309c349,350
< * int foo = mt.nextInt(32);
---
> * int foo = mt.nextInt(32);
> *
311,313c352,355
< * @param bits The number of significant bits desired in the output.
< * @return The next value in the pseudo random sequence with the
< * specified number of bits in the lower part of the integer.
---
> * @param bits
> * The number of significant bits desired in the output.
> * @return The next value in the pseudo random sequence with the specified
> * number of bits in the lower part of the integer.
318c360
< if (mti >= N) { // generate N words at one time
---
> if (mti >= N) { // generate N words at one time
327,330c369,372
<
< for (kk = 0; kk < N-M; kk++) {
< y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
< mt[kk] = mt[kk+M] ^ (y >>> 1) ^ MAGIC[y & 0x1];
---
>
> for (kk = 0; kk < N - M; kk++) {
> y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
> mt[kk] = mt[kk + M] ^ (y >>> 1) ^ MAGIC[y & 0x1];
332,334c374,376
< for (;kk < N-1; kk++) {
< y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
< mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ MAGIC[y & 0x1];
---
> for (; kk < N - 1; kk++) {
> y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
> mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ MAGIC[y & 0x1];
336,337c378,379
< y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
< mt[N-1] = mt[M-1] ^ (y >>> 1) ^ MAGIC[y & 0x1];
---
> y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
> mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ MAGIC[y & 0x1];
341c383
<
---
>
350c392
< return (y >>> (32-bits));
---
> return (y >>> (32 - bits));
354c396
< // byte[] into an int[] in little endian ordering.
---
> // byte[] into an int[] in little endian ordering.
357,362c399,403
< * This simply utility method can be used in cases where a byte
< * array of seed data is to be used to repeatedly re-seed the
< * random number sequence. By packing the byte array into an
< * integer array first, using this method, and then invoking
< * setSeed() with that; it removes the need to re-pack the byte
< * array each time setSeed() is called.
---
> * This simply utility method can be used in cases where a byte array of
> * seed data is to be used to repeatedly re-seed the random number sequence.
> * By packing the byte array into an integer array first, using this method,
> * and then invoking setSeed() with that; it removes the need to re-pack the
> * byte array each time setSeed() is called.
364,366c405,411
< * If the length of the byte array is not a multiple of 4 then
< * it is implicitly padded with zeros as necessary. For example:
< *
byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }--- > * If the length of the byte array is not a multiple of 4 then it is > * implicitly padded with zeros as necessary. For example: > * > *
> * byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 } > *> * 368c413,416 < *
int[] { 0x04030201, 0x00000605 }--- > * > *
> * int[] { 0x04030201, 0x00000605 } > *370,373c418,420 < * Note that this method will not complain if the given byte array < * is empty and will produce an empty integer array, but the < * setSeed() method will throw an exception if the empty integer < * array is passed to it. --- > * Note that this method will not complain if the given byte array is empty > * and will produce an empty integer array, but the setSeed() method will > * throw an exception if the empty integer array is passed to it. 375c422,423 < * @param buf The non-null byte array to be packed. --- > * @param buf > * The non-null byte array to be packed. 377c425,426 < * @throws NullPointerException if the given byte array is null. --- > * @throws NullPointerException > * if the given byte array is null. 380c429 < int k, blen = buf.length, ilen = ((buf.length+3) >>> 2); --- > int k, blen = buf.length, ilen = ((buf.length + 3) >>> 2); 383,385c432,437 < int m = (n+1) << 2; < if (m > blen) m = blen; < for (k = buf[--m]&0xff; (m & 0x3) != 0; k = (k << 8) | buf[--m]&0xff); --- > int m = (n + 1) << 2; > if (m > blen) > m = blen; > for (k = buf[--m] & 0xff; (m & 0x3) != 0; k = (k << 8) | buf[--m] > & 0xff) > ;