Package org.opt4j.common.random
Class RandomDefault
java.lang.Object
java.util.Random
org.opt4j.common.random.Rand
org.opt4j.common.random.RandomMersenneTwister
org.opt4j.common.random.RandomDefault
- All Implemented Interfaces:
Serializable
,RandomGenerator
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.random.RandomGenerator
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected int
next
(int bits) This method forms the basis for generating a pseudo random number sequence from this class.static int[]
pack
(byte[] buf) 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.void
setSeed
(byte[] buf) This method resets the state of this instance using the byte array of seed data provided.void
setSeed
(int[] buf) This method resets the state of this instance using the integer array of seed data provided.void
setSeed
(long seed) This method resets the state of this instance using the 64 bits of seed data provided.Methods inherited from class java.util.Random
doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, nextBoolean, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.random.RandomGenerator
isDeprecated, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextGaussian, nextInt, nextLong, nextLong
-
Constructor Details
-
RandomDefault
public RandomDefault()Constructs a newRandomDefault
.
-
-
Method Details
-
setSeed
public void setSeed(long seed) 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.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.
-
setSeed
public void setSeed(byte[] buf) 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 efficient.- Parameters:
buf
- The non-empty byte array of seed information.- Throws:
NullPointerException
- if the buffer is null.IllegalArgumentException
- if the buffer has zero length.
-
setSeed
public void setSeed(int[] buf) 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.- Parameters:
buf
- The non-empty integer array of seed information.- Throws:
NullPointerException
- if the buffer is null.IllegalArgumentException
- if the buffer has zero length.
-
next
protected int next(int bits) This method forms the basis for generating a pseudo random number 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.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:
mt.setSeed(12345); int foo = mt.nextInt(16) + (mt.nextInt(16) << 16);
will not give the same result asmt.setSeed(12345); int foo = mt.nextInt(32);
-
pack
public static int[] pack(byte[] buf) 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.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 }
becomesint[] { 0x04030201, 0x00000605 }
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.
- Parameters:
buf
- The non-null byte array to be packed.- Returns:
- A non-null integer array of the packed bytes.
- Throws:
NullPointerException
- if the given byte array is null.
-