Package com.github.tommyettinger.random
Class ArchivalWrapper
java.lang.Object
java.util.Random
com.github.tommyettinger.random.EnhancedRandom
com.github.tommyettinger.random.ArchivalWrapper
- All Implemented Interfaces:
Externalizable
,Serializable
,RandomGenerator
Wraps another
EnhancedRandom
and stores every long
it returns from nextLong()
in a LongSequence archive
. If you don't want to store anymore, but still want the same state and other
behavior of an ArchivalWrapper, you can assign LongSequence.NO_OP
to the archive
field. This can be
critical, because just generating numbers non-stop and storing all of them would exhaust most computers' available
memory in minutes, if not seconds.- 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
ConstructorDescriptionArchivalWrapper
(EnhancedRandom wrapped) ArchivalWrapper
(EnhancedRandom wrapped, LongSequence archive) -
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
Creates aKnownSequenceRandom
that will repeat from a copy of the currentarchive
.long
getSelectedState
(int selection) Gets a selected state value from this EnhancedRandom.Creates a copy of the current LongSequencearchive
and returns it.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, "ArcW".int
hashCode()
long
nextLong()
Returns the next pseudorandom, uniformly distributedlong
value from this random number generator's sequence.Makes this ArchivalWrapper stop storing generated random numbers, and returns the currentLongSequence
this had before pausing.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
setArchive
(LongSequence archive) 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.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
NeedsLongSequence
registered, as well as 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, nextInt, nextLong, nextLong, nextSign, nextSignedInt, nextSignedInt, nextSignedLong, nextSignedLong, nextTriangular, nextTriangular, nextTriangular, nextTriangular, nextUnsignedInt, previousInt, previousLong, probit, randomElement, randomElement, seedFromMath, 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
-
wrapped
-
archive
-
-
Constructor Details
-
ArchivalWrapper
public ArchivalWrapper() -
ArchivalWrapper
-
ArchivalWrapper
-
-
Method Details
-
getTag
Gets the tag used to identify this type of EnhancedRandom, "ArcW".- Specified by:
getTag
in classEnhancedRandom
- Returns:
- the String constant "ArcW"
-
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
-
nextLong
public long nextLong()Returns the next pseudorandom, uniformly distributedlong
value from this random number generator's sequence. The general contract ofnextLong
is that onelong
value is pseudorandomly generated and returned.
The only methods that need to be implemented by this interface are this andcopy()
, though other methods can be implemented as appropriate for generators that, for instance, natively produce ints rather than longs.- Specified by:
nextLong
in interfaceRandomGenerator
- Specified by:
nextLong
in classEnhancedRandom
- Returns:
- the next pseudorandom, uniformly distributed
long
value from this random number generator's 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.
-
getSnapshot
Creates a copy of the current LongSequencearchive
and returns it.- Returns:
- a copy of the current archive
-
getRepeatableRandom
Creates aKnownSequenceRandom
that will repeat from a copy of the currentarchive
.- Returns:
- a new KnownSequenceRandom that will use a copy of the current archive
-
pauseStorage
Makes this ArchivalWrapper stop storing generated random numbers, and returns the currentLongSequence
this had before pausing. To resume where you started, callsetArchive(LongSequence)
with what this returned.- Returns:
- the LongSequence this used before pausing; this can be used to resume later from this point
-
getArchive
-
setArchive
-
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
-
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
-
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
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
-
writeExternal
NeedsLongSequence
registered, as well as 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
-
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
-