Package com.github.tommyettinger.random
Class ReverseWrapper
java.lang.Object
java.util.Random
com.github.tommyettinger.random.EnhancedRandom
com.github.tommyettinger.random.ReverseWrapper
- All Implemented Interfaces:
Externalizable
,Serializable
,RandomGenerator
A wrapper around a different
EnhancedRandom
object that runs it in reverse, calling
EnhancedRandom.previousLong()
instead anywhere nextLong()
would be called.
This can be useful to reverse a sequence of calls made to the wrapped generator earlier; the reversed
calls have to happen in reverse order to undo an operation such as a shuffle, typically.
Calling previousLong() is typically not as fast as nextLong(), but usually not by any large degree.- 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
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates a ReverseWrapper that wraps a DistinctRandom with a random seed.ReverseWrapper
(long seed) Creates a ReverseWrapper that wraps a DistinctRandom created withDistinctRandom(long)
.ReverseWrapper
(EnhancedRandom toWrap) This does not copytoWrap
, and uses a reference as-is, so this can be useful to reverse some series of calls made earlier to toWrap in a forward direction.ReverseWrapper
(ReverseWrapper other) The copy constructor. -
Method Summary
Modifier and TypeMethodDescriptioncopy()
Creates a new EnhancedRandom with identical states to this one, so if the same EnhancedRandom methods are called on this object and its copy (in the same order), the same outputs will be produced.boolean
long
getSelectedState
(int selection) Gets a selected state value from this EnhancedRandom.int
Gets the number of possible state variables that can be selected withgetSelectedState(int)
orsetSelectedState(int, long)
.getTag()
Gets the tag used to identify this type of EnhancedRandom, as a String.int
hashCode()
int
nextInt()
Returns the previous pseudorandom, uniformly distributedint
value from the wrapped random number generator's sequence.long
nextLong()
Returns the previous pseudorandom, uniformly distributedlong
value from the wrapped random number generator's sequence.int
Delegates to theEnhancedRandom.nextInt()
method of the wrapped generator; this is the counterpart to hownextInt()
here delegates to theEnhancedRandom.previousInt()
()} method of the wrapped generator.long
Delegates to theEnhancedRandom.nextLong()
method of the wrapped generator; this is the counterpart to hownextLong()
here delegates to theEnhancedRandom.previousLong()
method of the wrapped generator.void
The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays.void
setSeed
(long seed) Sets the seed of this random number generator using a singlelong
seed.void
setSelectedState
(int selection, long value) Sets a selected state value to the given longvalue
.void
setState
(long state) Sets each state variable to the givenstate
.void
setState
(long... states) Sets all state variables to alternating values chosen fromstates
.void
setState
(long stateA, long stateB) Sets each state variable to eitherstateA
orstateB
, alternating.void
setState
(long stateA, long stateB, long stateC) Sets each state variable tostateA
,stateB
, orstateC
, alternating.void
setState
(long stateA, long stateB, long stateC, long stateD) Sets each state variable tostateA
,stateB
,stateC
, orstateD
, alternating.void
setState
(long stateA, long stateB, long stateC, long stateD, long stateE) Sets each state variable tostateA
,stateB
,stateC
, orstateD
, alternating.void
setWrapped
(EnhancedRandom wrapped) void
shuffle
(boolean[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(boolean[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(byte[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(byte[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(char[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(char[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(double[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(double[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(float[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(float[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(int[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(int[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(long[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(long[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(short[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.void
shuffle
(short[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.<T> void
Shuffles the given List in-place pseudo-randomly, using this to determine how to shuffle.<T> void
Shuffles a section of the given List in-place pseudo-randomly, using this to determine how to shuffle.<T> void
shuffle
(T[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.<T> void
shuffle
(T[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle.long
skip
(long advance) Delegates to theEnhancedRandom.skip(long)
method if the wrapped generator, but passes it-advance
instead of advance without changes.stringDeserialize
(String data, com.github.tommyettinger.digital.Base base) Given a String in the format produced bystringSerialize(Base)
, and the sameBase
used by the serialization, this will attempt to set this EnhancedRandom object to match the state in the serialized data.stringSerialize
(com.github.tommyettinger.digital.Base base) Serializes the current state of this EnhancedRandom to a String that can be used byEnhancedRandom.stringDeserialize(String)
to load this state at another time.toString()
void
Needs the type ofwrapped
registered.Methods inherited from class com.github.tommyettinger.random.EnhancedRandom
areEqual, fixGamma, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, next, nextBoolean, nextBoolean, nextBytes, nextDouble, nextDouble, nextDouble, nextExclusiveDouble, nextExclusiveDouble, nextExclusiveDouble, nextExclusiveDoubleEquidistant, nextExclusiveFloat, nextExclusiveFloat, nextExclusiveFloat, nextExclusiveFloatEquidistant, nextExclusiveSignedDouble, nextExclusiveSignedFloat, nextFloat, nextFloat, nextFloat, nextGaussian, nextGaussian, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveFloat, nextInclusiveFloat, nextInclusiveFloat, nextInt, nextInt, nextLong, nextLong, nextSign, nextSignedInt, nextSignedInt, nextSignedLong, nextSignedLong, nextTriangular, nextTriangular, nextTriangular, nextTriangular, nextUnsignedInt, probit, randomElement, randomElement, seedFromMath, setWith, stringDeserialize, stringSerialize
Methods inherited from class java.util.Random
doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.random.RandomGenerator
isDeprecated, nextExponential
-
Field Details
-
wrapped
-
-
Constructor Details
-
ReverseWrapper
public ReverseWrapper()Creates a ReverseWrapper that wraps a DistinctRandom with a random seed. -
ReverseWrapper
public ReverseWrapper(long seed) Creates a ReverseWrapper that wraps a DistinctRandom created withDistinctRandom(long)
.- Parameters:
seed
- the seed that will be used for the wrapped DistinctRandom
-
ReverseWrapper
This does not copytoWrap
, and uses a reference as-is, so this can be useful to reverse some series of calls made earlier to toWrap in a forward direction.- Parameters:
toWrap
- a non-null EnhancedRandom, preferably not also a wrapper
-
ReverseWrapper
The copy constructor.- Parameters:
other
- a non-null ReverseWrapper; itswrapped EnhancedRandom
must also be non-null.
-
-
Method Details
-
getWrapped
-
setWrapped
-
getStateCount
public int getStateCount()Gets the number of possible state variables that can be selected withgetSelectedState(int)
orsetSelectedState(int, long)
. This defaults to returning 0, making no state variable available for reading or writing. An implementation that has only onelong
state, likeDistinctRandom
generator, should return1
. A generator that permits setting two differentlong
values, likeLaserRandom
, should return2
. Much larger values are possible for types like the Mersenne Twister or some CMWC generators.- Overrides:
getStateCount
in classEnhancedRandom
- Returns:
- the non-negative number of selections possible for state variables
-
getTag
Gets the tag used to identify this type of EnhancedRandom, as a String. This tag should be unique, and for uniformity purposes, all tags used in this library are 4 characters long. User-defined tags should have a different length.- Specified by:
getTag
in classEnhancedRandom
- Returns:
- a unique String identifier for this type of EnhancedRandom; usually 4 chars long.
-
setSeed
public void setSeed(long seed) Sets the seed of this random number generator using a singlelong
seed. This should behave exactly the same as if a new object of this type was created with the constructor that takes a singlelong
value. This does not necessarily assign the state variable(s) of the implementation with the exact contents of seed, sogetSelectedState(int)
should not be expected to returnseed
after this, though it may. If this implementation has more than onelong
of state, then the expectation is that none of those state variables will be exactly equal toseed
(almost all the time).- Specified by:
setSeed
in classEnhancedRandom
- Parameters:
seed
- the initial seed
-
getSelectedState
public long getSelectedState(int selection) Gets a selected state value from this EnhancedRandom. The number of possible selections is up to the implementing class, and is accessible viagetStateCount()
, but negative values forselection
are typically not tolerated. This should return the exact value of the selected state, assuming it is implemented. The default implementation throws an UnsupportedOperationException, and implementors only have to allow reading the state if they choose to implement this differently. If this method is intended to be used,getStateCount()
must also be implemented.- Overrides:
getSelectedState
in classEnhancedRandom
- Parameters:
selection
- used to select which state variable to get; generally non-negative- Returns:
- the exact value of the selected state
-
setSelectedState
public void setSelectedState(int selection, long value) Sets a selected state value to the given longvalue
. The number of possible selections is up to the implementing class, but negative values forselection
are typically not tolerated. Implementors are permitted to changevalue
if it is not valid, but they should not alter it if it is valid. The public implementation callssetSeed(long)
withvalue
, which doesn't need changing if the generator has one state that is set verbatim by setSeed(). Otherwise, this method should be implemented whengetSelectedState(int)
is and the state is allowed to be set by users. Having accurate ways to get and set the full state of a random number generator makes it much easier to serialize and deserialize that class.- Overrides:
setSelectedState
in classEnhancedRandom
- Parameters:
selection
- used to select which state variable to set; generally non-negativevalue
- the exact value to use for the selected state, if valid
-
setState
public void setState(long state) Sets each state variable to the givenstate
. IfgetStateCount()
is 1, then this should set the whole state to the given value usingsetSelectedState(int, long)
. If getStateCount() is more than 1, then all states will be set in the same way (using setSelectedState(), all tostate
).- Overrides:
setState
in classEnhancedRandom
- Parameters:
state
- the long value to use for each state variable
-
setState
public void setState(long stateA, long stateB) Sets each state variable to eitherstateA
orstateB
, alternating. This usessetSelectedState(int, long)
to set the values. If there is one state variable (getStateCount()
is 1), then this only sets that state variable to stateA. If there are two state variables, the first is set to stateA, and the second to stateB. If there are more, it reuses stateA, then stateB, then stateA, and so on until all variables are set.- Overrides:
setState
in classEnhancedRandom
- Parameters:
stateA
- the long value to use for states at index 0, 2, 4, 6...stateB
- the long value to use for states at index 1, 3, 5, 7...
-
setState
public void setState(long stateA, long stateB, long stateC) Sets each state variable tostateA
,stateB
, orstateC
, alternating. This usessetSelectedState(int, long)
to set the values. If there is one state variable (getStateCount()
is 1), then this only sets that state variable to stateA. If there are two state variables, the first is set to stateA, and the second to stateB. With three state variables, the first is set to stateA, the second to stateB, and the third to stateC. If there are more, it reuses stateA, then stateB, then stateC, then stateA, and so on until all variables are set.- Overrides:
setState
in classEnhancedRandom
- Parameters:
stateA
- the long value to use for states at index 0, 3, 6, 9...stateB
- the long value to use for states at index 1, 4, 7, 10...stateC
- the long value to use for states at index 2, 5, 8, 11...
-
setState
public void setState(long stateA, long stateB, long stateC, long stateD) Sets each state variable tostateA
,stateB
,stateC
, orstateD
, alternating. This usessetSelectedState(int, long)
to set the values. If there is one state variable (getStateCount()
is 1), then this only sets that state variable to stateA. If there are two state variables, the first is set to stateA, and the second to stateB. With three state variables, the first is set to stateA, the second to stateB, and the third to stateC. With four state variables, the first is set to stateA, the second to stateB, the third to stateC, and the fourth to stateD. If there are more, it reuses stateA, then stateB, then stateC, then stateD, then stateA, and so on until all variables are set.- Overrides:
setState
in classEnhancedRandom
- Parameters:
stateA
- the long value to use for states at index 0, 4, 8, 12...stateB
- the long value to use for states at index 1, 5, 9, 13...stateC
- the long value to use for states at index 2, 6, 10, 14...stateD
- the long value to use for states at index 3, 7, 11, 15...
-
setState
public void setState(long stateA, long stateB, long stateC, long stateD, long stateE) Sets each state variable tostateA
,stateB
,stateC
, orstateD
, alternating. This usessetSelectedState(int, long)
to set the values. If there is one state variable (getStateCount()
is 1), then this only sets that state variable to stateA. If there are two state variables, the first is set to stateA, and the second to stateB. With three state variables, the first is set to stateA, the second to stateB, and the third to stateC. With four state variables, the first is set to stateA, the second to stateB, the third to stateC, and the fourth to stateD. If there are more, it reuses stateA, then stateB, then stateC, then stateD, then stateA, and so on until all variables are set.- Overrides:
setState
in classEnhancedRandom
- Parameters:
stateA
- the long value to use for states at index 0, 5, 10, 15...stateB
- the long value to use for states at index 1, 6, 11, 16...stateC
- the long value to use for states at index 2, 7, 12, 17...stateD
- the long value to use for states at index 3, 8, 13, 18...stateE
- the long value to use for states at index 4, 9, 14, 19...
-
setState
public void setState(long... states) Sets all state variables to alternating values chosen fromstates
. If states is empty, then this does nothing, and leaves the current generator unchanged. This works for generators with anygetStateCount()
, but may allocate an array if states is used as a varargs (you can pass an existing array without needing to allocate). This usessetSelectedState(int, long)
to change the states.- Overrides:
setState
in classEnhancedRandom
- Parameters:
states
- an array or varargs of long values to use as states
-
nextLong
public long nextLong()Returns the previous pseudorandom, uniformly distributedlong
value from the wrapped random number generator's sequence. The general contract ofnextLong
is that onelong
value is pseudorandomly generated and returned.- Specified by:
nextLong
in interfaceRandomGenerator
- Specified by:
nextLong
in classEnhancedRandom
- Returns:
- the previous pseudorandom, uniformly distributed
long
value from the wrapped random number generator's sequence
-
nextInt
public int nextInt()Returns the previous pseudorandom, uniformly distributedint
value from the wrapped random number generator's sequence. The general contract ofnextInt
is that oneint
value is pseudorandomly generated and returned. All 232 possibleint
values are produced with (approximately) equal probability.- Specified by:
nextInt
in interfaceRandomGenerator
- Overrides:
nextInt
in classEnhancedRandom
- Returns:
- the previous pseudorandom, uniformly distributed
int
value from the wrapped random number generator's sequence
-
previousLong
public long previousLong()Delegates to theEnhancedRandom.nextLong()
method of the wrapped generator; this is the counterpart to hownextLong()
here delegates to theEnhancedRandom.previousLong()
method of the wrapped generator.- Overrides:
previousLong
in classEnhancedRandom
- Returns:
- the number that
wrapped.nextLong()
would produce
-
previousInt
public int previousInt()Delegates to theEnhancedRandom.nextInt()
method of the wrapped generator; this is the counterpart to hownextInt()
here delegates to theEnhancedRandom.previousInt()
()} method of the wrapped generator.- Overrides:
previousInt
in classEnhancedRandom
- Returns:
- the number that
wrapped.nextLong()
would produce
-
skip
public long skip(long advance) Delegates to theEnhancedRandom.skip(long)
method if the wrapped generator, but passes it-advance
instead of advance without changes. This makes it skip in the reverse direction.- Overrides:
skip
in classEnhancedRandom
- Parameters:
advance
- Number of future generations to skip over; can be negative to backtrack, 0 gets the most-recently-generated number- Returns:
- the random long generated after skipping forward or backwards by
advance
numbers
-
shuffle
public void shuffle(long[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public void shuffle(byte[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public void shuffle(int[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public void shuffle(short[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public void shuffle(float[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public void shuffle(double[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public void shuffle(boolean[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public void shuffle(char[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public <T> void shuffle(T[] items, int offset, int length) Shuffles a section of the given array in-place pseudo-randomly, using this to determine how to shuffle. Similarly to hownextLong()
uses the wrapped generator'sEnhancedRandom.previousLong()
, this shuffles in reverse order. This allows you to shuffle an array using one of the wrapped generator's shuffle() methods, then later undo that shuffle by using a ReverseWrapper's shuffle method.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
shuffle
public void shuffle(int[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an int array; must be non-null
-
shuffle
public void shuffle(long[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a long array; must be non-null
-
shuffle
public void shuffle(float[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a float array; must be non-null
-
shuffle
public void shuffle(char[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a char array; must be non-null
-
shuffle
public void shuffle(byte[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a byte array; must be non-null
-
shuffle
public void shuffle(double[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a double array; must be non-null
-
shuffle
public void shuffle(short[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a short array; must be non-null
-
shuffle
public void shuffle(boolean[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a boolean array; must be non-null
-
shuffle
public <T> void shuffle(T[] items) Shuffles the given array in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- an array of some reference type; must be non-null but may contain null items
-
shuffle
Shuffles the given List in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a List of some typeT
; must be non-null but may contain null items
-
shuffle
Shuffles a section of the given List in-place pseudo-randomly, using this to determine how to shuffle.- Overrides:
shuffle
in classEnhancedRandom
- Parameters:
items
- a List of some typeT
; must be non-null but may contain null itemsoffset
- the index of the first element of the array that can be shuffledlength
- the length of the section to shuffle
-
copy
Creates a new EnhancedRandom with identical states to this one, so if the same EnhancedRandom methods are called on this object and its copy (in the same order), the same outputs will be produced. This is not guaranteed to copy the inherited state of any parent class, so if you call methods that are only implemented by a superclass (likeRandom
) and not this one, the results may differ.- Specified by:
copy
in classEnhancedRandom
- Returns:
- a deep copy of this EnhancedRandom.
-
stringSerialize
Serializes the current state of this EnhancedRandom to a String that can be used byEnhancedRandom.stringDeserialize(String)
to load this state at another time.- Overrides:
stringSerialize
in classEnhancedRandom
- Parameters:
base
- which Base to use, from the "digital" library, such asBase.BASE10
- Returns:
- a String storing all data from the EnhancedRandom part of this generator
-
stringDeserialize
Given a String in the format produced bystringSerialize(Base)
, and the sameBase
used by the serialization, this will attempt to set this EnhancedRandom object to match the state in the serialized data. This only works if this EnhancedRandom is the same implementation that was serialized, and also needs the Bases to be identical. Returns this EnhancedRandom, after possibly changing its state.- Overrides:
stringDeserialize
in classEnhancedRandom
- Parameters:
data
- a String probably produced bystringSerialize(Base)
base
- which Base to use, from the "digital" library, such asBase.BASE10
- Returns:
- this, after setting its state
-
equals
-
hashCode
public int hashCode() -
toString
-
readExternal
The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.- Specified by:
readExternal
in interfaceExternalizable
- Overrides:
readExternal
in classEnhancedRandom
- Parameters:
in
- the stream to read data from in order to restore the object- Throws:
IOException
- if I/O errors occurClassNotFoundException
-
writeExternal
Needs the type ofwrapped
registered.- Specified by:
writeExternal
in interfaceExternalizable
- Overrides:
writeExternal
in classEnhancedRandom
- Parameters:
out
- the stream to write the object to- Throws:
IOException
- Includes any I/O exceptions that may occur
-