Package com.github.tommyettinger.random
Class KnownSequenceRandom
java.lang.Object
java.util.Random
com.github.tommyettinger.random.EnhancedRandom
com.github.tommyettinger.random.KnownSequenceRandom
- All Implemented Interfaces:
Externalizable
,Serializable
,RandomGenerator
A non-random number generator that simply repeats the next of a sequence of
long
values
every time nextLong()
is called. Because other methods rely on nextLong()'s exact output,
even if they don't use all of it, storing long values is usually enough to repeat any sequence of
calls made to a generator. This is meant to be useful for things like unit tests or procedural
generation, where a particular group of outputs should be exactly replicable.- 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
Modifier and TypeFieldDescriptionint
The index into the sequence of values this draws results from.The sequence of long values this draws results from. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionappendSerialized
(StringBuilder sb, com.github.tommyettinger.digital.Base base) 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.boolean
getKnown()
Returns the current known sequence this draws its results from.long
getSelectedState
(int selection) Gets the current index/position in the known sequence.int
Returns 1, referring to the one state this changes on its own (index
).getTag()
Gets the tag used to identify this type of EnhancedRandom, which is "KnSR".int
hashCode()
long
nextLong()
Returns the nextlong
value from this generator's sequence.long
Optional; moves the state to its previous value and returns the previous long that would have been produced bynextLong()
.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
setKnown
(LongSequence known) Changes the known sequence this draws its results from.void
setSeed
(long position) Sets the position of the iteration this makes through its known sequence.void
setSelectedState
(int selection, long value) Sets the index/position in the known sequence, ifvalue
is at least equal to 0 and less thanknown.size
.void
setState
(long state) Sets the index/position in the known sequence, ifstate
is at least equal to 0 and less thanknown.size
.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
The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.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, nextInt, nextLong, nextLong, nextSign, nextSignedInt, nextSignedInt, nextSignedLong, nextSignedLong, nextTriangular, nextTriangular, nextTriangular, nextTriangular, nextUnsignedInt, previousInt, probit, randomElement, randomElement, seedFromMath, setState, setState, setState, setState, setState, setWith, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, shuffle, skip, 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
-
known
The sequence of long values this draws results from. This is public for ease of serialization, but it should typically not be modified in a way that changes its length unless you callsetState(long)
after. -
index
public int indexThe index into the sequence of values this draws results from. This is public for ease of serialization. To change this, usingsetState(long)
is recommended.
-
-
Constructor Details
-
KnownSequenceRandom
public KnownSequenceRandom() -
KnownSequenceRandom
-
KnownSequenceRandom
-
-
Method Details
-
getTag
Gets the tag used to identify this type of EnhancedRandom, which is "KnSR".- Specified by:
getTag
in classEnhancedRandom
- Returns:
- the constant String "KnSR"
-
setSeed
public void setSeed(long position) Sets the position of the iteration this makes through its known sequence.- Specified by:
setSeed
in classEnhancedRandom
- Parameters:
position
- usually a positive int less thanknown.size
, but this technically can be any long
-
nextLong
public long nextLong()Returns the nextlong
value from this generator's sequence. This "generator" only cycles through a known sequence of values; it does not actually do any math to generate random numbers.
All other "random" number generation methods in this class call this method, so if another class also relies on nextLong() for all randomness, then recording each of those nextLong() outputs in a LongSequence will allow a section of a generator to be played back more or less exactly.- Specified by:
nextLong
in interfaceRandomGenerator
- Specified by:
nextLong
in classEnhancedRandom
- Returns:
- the next long from the known sequence
-
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.
-
getStateCount
public int getStateCount()Returns 1, referring to the one state this changes on its own (index
). This does not include the potentially many values in the known sequence.- Overrides:
getStateCount
in classEnhancedRandom
- Returns:
- one (1)
-
getSelectedState
public long getSelectedState(int selection) Gets the current index/position in the known sequence.- Overrides:
getSelectedState
in classEnhancedRandom
- Parameters:
selection
- ignored- Returns:
- the exact value of
index
-
setSelectedState
public void setSelectedState(int selection, long value) Sets the index/position in the known sequence, ifvalue
is at least equal to 0 and less thanknown.size
. If value is outside that range, this can assign any value inside the range to the index. Ifknown.size
is 0 or less, this always assigns 0 to index (anticipating some change to the known sequence before it is used, hopefully).- Overrides:
setSelectedState
in classEnhancedRandom
- Parameters:
selection
- ignoredvalue
- the value to use for index, if at least equal to 0 and less thanknown.size
-
setState
public void setState(long state) Sets the index/position in the known sequence, ifstate
is at least equal to 0 and less thanknown.size
. If state is outside that range, this can assign any value inside the range to the index. Ifknown.size
is 0 or less, this always assigns 0 to index (anticipating some change to the known sequence before it is used, hopefully).- Overrides:
setState
in classEnhancedRandom
- Parameters:
state
- the value to use for index, if at least equal to 0 and less thanknown.size
-
previousLong
public long previousLong()Optional; moves the state to its previous value and returns the previous long that would have been produced bynextLong()
. This is often equivalent to callingEnhancedRandom.skip(long)
with -1L, but not always; some generators can't efficiently skip long distances, but can step back by one value.The public implementation calls
EnhancedRandom.skip(long)
with -1L, and if skip() has not been implemented differently, then it will throw an UnsupportedOperationException.- Overrides:
previousLong
in classEnhancedRandom
- Returns:
- the previous number this would have produced with
nextLong()
-
getKnown
Returns the current known sequence this draws its results from. While this can be modified, changing the length of the returned sequence is strongly discouraged, since it can cause indices to go out-of-bounds. If you do change the length of the returned sequence, you should callsetState(long)
before obtaining any values from this generator.- Returns:
- the current known sequence of long results
-
setKnown
Changes the known sequence this draws its results from. If the previous known sequence has a different length from the new known sequence, this resets the index in the sequence to 0.- Parameters:
known
- a LongSequence that cannot be null and should generally not be empty
-
appendSerialized
-
appendSerialized
-
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
public KnownSequenceRandom 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. 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
-
writeExternal
The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.- 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
-
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
-
toString
-
equals
-
hashCode
public int hashCode()
-