Class DistributedRandom
- All Implemented Interfaces:
Externalizable,Serializable,RandomGenerator
Distribution to distribute any floats, ints, or doubles as by that
distribution.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumOne of two possible modes this can use to limit the range of a Distribution.Nested classes/interfaces inherited from interface java.util.random.RandomGenerator
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Distributionprotected DistributedRandom.ReductionMode -
Constructor Summary
ConstructorsConstructorDescriptionDistributedRandom(long seed) DistributedRandom(long stateA, long stateB, long stateC, long stateD) DistributedRandom(Distribution distribution, DistributedRandom.ReductionMode reductionMode) DistributedRandom(Distribution distribution, DistributedRandom.ReductionMode reductionMode, long seed) DistributedRandom(Distribution distribution, DistributedRandom.ReductionMode reductionMode, long stateA, long stateB, long stateC, long stateD) DistributedRandom(Distribution distribution, DistributedRandom.ReductionMode reductionMode, EnhancedRandom random) Creates a DistributedRandom that follows the given Distribution (copied), limiting its results using the given ReductionMode, and uses a direct reference to the given EnhancedRandom.DistributedRandom(EnhancedRandom random) Creates a DistributedRandom that follows a ContinuousUniformDistribution, limiting its results usingDistributedRandom.ReductionMode.FRACTION, and uses a direct reference to the given EnhancedRandom. -
Method Summary
Modifier and TypeMethodDescription<T extends CharSequence & Appendable>
TappendSerialized(T sb, com.github.tommyettinger.digital.Base base) Serializes the current state of this EnhancedRandom and appends it to an Appendable CharSequence (such as a StringBuilder), which may be used byEnhancedRandom.stringDeserialize(String)to load this state at another time.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.Depends on thedistribution'sgenerator'sresult.longgetSelectedState(int selection) Gets a selected state value from this EnhancedRandom.intGets the number of possible state variables that can be selected withEnhancedRandom.getSelectedState(int)orEnhancedRandom.setSelectedState(int, long).getTag()Gets the tag used to identify this type of EnhancedRandom, as a String.booleanDepends on thedistribution'sgenerator'sresult.intnext(int bits) Generates the next pseudorandom number with a specific maximum size in bits (not a max number).booleanReturns the next pseudorandom, uniformly distributedbooleanvalue from this random number generator's sequence.voidnextBytes(byte[] bytes) Generates random bytes and places them into a user-supplied byte array.doubleReturns the next pseudorandom, uniformly distributeddoublevalue between0.0(inclusive) and1.0(exclusive) from this random number generator's sequence.doubleThis does not use the optimizations fromEnhancedRandom.nextExclusiveDouble(), because those aren't reasonable when distributed.floatThis acts the same asEnhancedRandom.nextExclusiveFloatEquidistant(); it does not use the optimizations fromEnhancedRandom.nextExclusiveFloat(), because those aren't reasonable when distributed.doubleThis differs from the implementation used by other EnhancedRandom types in that the distribution applies to the(0,1)range as normal, but half the time this will negate that distribution, so it is on(-1,0)and the high and low ends are reversed.floatThis differs from the implementation used by other EnhancedRandom types in that the distribution applies to the(0,1)range as normal, but half the time this will negate that distribution, so it is on(-1,0)and the high and low ends are reversed.floatReturns the next pseudorandom, uniformly distributedfloatvalue between0.0(inclusive) and1.0(exclusive) from this random number generator's sequence.doubleThis runsDistributor.probitD(double)on a distributed double this produces.intnextInt()Returns the next pseudorandom, uniformly distributedintvalue from this random number generator's sequence.intnextInt(int bound) Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.longnextLong()Returns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence.intnextSignedInt(int outerBound) Returns a pseudorandom, uniformly distributedintvalue between an inner bound of 0 (inclusive) and the specifiedouterBound(exclusive).voidThe 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.voidsetDistribution(Distribution distribution) voidsetRandom(EnhancedRandom random) voidsetReduction(DistributedRandom.ReductionMode reduction) voidsetSeed(long seed) Sets the seed of this random number generator using a singlelongseed.voidsetSelectedState(int selection, long value) Sets a selected state value to the given longvalue.voidsetState(long stateA) Sets each state variable to the givenstate.voidsetState(long stateA, long stateB) Sets each state variable to eitherstateAorstateB, alternating.voidsetState(long stateA, long stateB, long stateC) Sets each state variable tostateA,stateB, orstateC, alternating.voidsetState(long stateA, long stateB, long stateC, long stateD) 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 byEnhancedRandom.stringSerialize(Base), and the sameBaseused 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()voidNeeds the type ofdistributionregistered.Methods inherited from class com.github.tommyettinger.random.EnhancedRandom
appendSerialized, areEqual, fixGamma, fixGamma, lcm, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, nextBoolean, nextDouble, nextDouble, nextExclusiveDouble, nextExclusiveDouble, nextExclusiveDoubleEquidistant, nextExclusiveFloat, nextExclusiveFloat, nextExclusiveFloatEquidistant, nextExponential, nextFloat, nextFloat, nextGaussian, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveFloat, nextInclusiveFloat, nextInclusiveFloat, nextInt, nextLong, nextLong, nextSign, nextSignedInt, nextSignedLong, nextSignedLong, nextTriangular, nextTriangular, nextTriangular, nextTriangular, nextUnsignedInt, previousInt, previousLong, probit, randomElement, randomElement, rateGamma, seedFromMath, 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, stringSerializeMethods inherited from class java.util.Random
doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.random.RandomGenerator
isDeprecated
-
Field Details
-
distribution
-
reduction
-
-
Constructor Details
-
DistributedRandom
public DistributedRandom() -
DistributedRandom
public DistributedRandom(long seed) -
DistributedRandom
Creates a DistributedRandom that follows a ContinuousUniformDistribution, limiting its results usingDistributedRandom.ReductionMode.FRACTION, and uses a direct reference to the given EnhancedRandom. You can copy the EnhancedRandom if you want it to change independently of the original EnhancedRandom, usingEnhancedRandom.copy(). This is probably not very useful on its own, since you will probably want to change the distribution (usingsetDistribution(Distribution)), and that also changes the EnhancedRandom that was assigned here.- Parameters:
random- referenced directly; if you don't want this, use aEnhancedRandom.copy()
-
DistributedRandom
public DistributedRandom(long stateA, long stateB, long stateC, long stateD) -
DistributedRandom
-
DistributedRandom
public DistributedRandom(Distribution distribution, DistributedRandom.ReductionMode reductionMode, long seed) -
DistributedRandom
public DistributedRandom(Distribution distribution, DistributedRandom.ReductionMode reductionMode, EnhancedRandom random) Creates a DistributedRandom that follows the given Distribution (copied), limiting its results using the given ReductionMode, and uses a direct reference to the given EnhancedRandom. You can copy the EnhancedRandom if you want it to change independently of the original EnhancedRandom, usingEnhancedRandom.copy().- Parameters:
distribution- a Distribution that will be copied; the copy's generator will be reassigned.reductionMode- how to reduce values outside the 0 to 1 range, as an enum constantrandom- referenced directly; if you don't want this, use aEnhancedRandom.copy()
-
DistributedRandom
public DistributedRandom(Distribution distribution, DistributedRandom.ReductionMode reductionMode, long stateA, long stateB, long stateC, long stateD)
-
-
Method Details
-
getTag
Description copied from class:EnhancedRandomGets 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:
getTagin classEnhancedRandom- Returns:
- a unique String identifier for this type of EnhancedRandom; usually 4 chars long.
-
mainlyGeneratesInt
public boolean mainlyGeneratesInt()Depends on thedistribution'sgenerator'sresult.- Overrides:
mainlyGeneratesIntin classEnhancedRandom- Returns:
- whatever
getDistribution().generator.mainlyGeneratesInt()returns - See Also:
-
getMinimumPeriod
Depends on thedistribution'sgenerator'sresult.- Overrides:
getMinimumPeriodin classEnhancedRandom- Returns:
- whatever
getDistribution().generator.getMinimumPeriod()returns
-
getReduction
-
setReduction
-
nextLong
public long nextLong()Description copied from class:EnhancedRandomReturns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence. The general contract ofnextLongis that onelongvalue is pseudorandomly generated and returned.
The only methods that need to be implemented by this interface are this andEnhancedRandom.copy(), though other methods can be implemented as appropriate for generators that, for instance, natively produce ints rather than longs.- Specified by:
nextLongin interfaceRandomGenerator- Specified by:
nextLongin classEnhancedRandom- Returns:
- the next pseudorandom, uniformly distributed
longvalue from this random number generator's sequence
-
next
public int next(int bits) Description copied from class:EnhancedRandomGenerates the next pseudorandom number with a specific maximum size in bits (not a max number). If you want to get a random number in a range, you should usually useEnhancedRandom.nextInt(int)instead. For some specific cases, this method is more efficient and less biased thanEnhancedRandom.nextInt(int). Forbitsvalues between 1 and 30, this should be similar in effect tonextInt(1 << bits); though it won't typically produce the same values, they will have the correct range. Ifbitsis 31, this can return any non-negativeint; note thatnextInt(1 << 31)won't behave this way because1 << 31is negative. Ifbitsis 32 (or 0), this can return anyint.The general contract of
nextis that it returns anintvalue and if the argumentbitsis between1and32(inclusive), then that many low-order bits of the returned value will be (approximately) independently chosen bit values, each of which is (approximately) equally likely to be0or1.Note that you can give this values for
bitsthat are outside its expected range of 1 to 32, but the value used, as long as bits is positive, will effectively bebits % 32. As stated before, a value of 0 for bits is the same as a value of 32.- Overrides:
nextin classEnhancedRandom- Parameters:
bits- the amount of random bits to request, from 1 to 32- Returns:
- the next pseudorandom value from this random number generator's sequence
-
nextDouble
public double nextDouble()Description copied from class:EnhancedRandomReturns the next pseudorandom, uniformly distributeddoublevalue between0.0(inclusive) and1.0(exclusive) from this random number generator's sequence.The general contract of
nextDoubleis that onedoublevalue, chosen (approximately) uniformly from the range0.0d(inclusive) to1.0d(exclusive), is pseudorandomly generated and returned.The public implementation uses the upper 53 bits of
EnhancedRandom.nextLong(), with an unsigned right shift and a multiply by a very small double (1.1102230246251565E-16, or0x1p-53). It should perform well if nextLong() performs well, and is expected to perform less well if the generator naturally produces 32 or fewer bits at a time.- Specified by:
nextDoublein interfaceRandomGenerator- Overrides:
nextDoublein classEnhancedRandom- Returns:
- the next pseudorandom, uniformly distributed
doublevalue between0.0and1.0from this random number generator's sequence
-
nextFloat
public float nextFloat()Description copied from class:EnhancedRandomReturns the next pseudorandom, uniformly distributedfloatvalue between0.0(inclusive) and1.0(exclusive) from this random number generator's sequence.The general contract of
nextFloatis that onefloatvalue, chosen (approximately) uniformly from the range0.0f(inclusive) to1.0f(exclusive), is pseudorandomly generated and returned. All 224 possiblefloatvalues of the form m x 2-24, where m is a positive integer less than 224, are produced with (approximately) equal probability.The public implementation uses the upper 24 bits of
EnhancedRandom.nextLong(), with an unsigned right shift and a multiply by a very small float (5.9604645E-8for0x1p-24f). It tends to be fast if nextLong() is fast, but alternative implementations could use 24 bits ofEnhancedRandom.nextInt()(or justEnhancedRandom.next(int), giving it24) if that generator doesn't efficiently generate 64-bit longs.- Specified by:
nextFloatin interfaceRandomGenerator- Overrides:
nextFloatin classEnhancedRandom- Returns:
- the next pseudorandom, uniformly distributed
floatvalue between0.0and1.0from this random number generator's sequence
-
nextBytes
public void nextBytes(byte[] bytes) Description copied from class:EnhancedRandomGenerates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.- Specified by:
nextBytesin interfaceRandomGenerator- Overrides:
nextBytesin classEnhancedRandom- Parameters:
bytes- the byte array to fill with random bytes
-
nextInt
public int nextInt()Description copied from class:EnhancedRandomReturns the next pseudorandom, uniformly distributedintvalue from this random number generator's sequence. The general contract ofnextIntis that oneintvalue is pseudorandomly generated and returned. All 232 possibleintvalues are produced with (approximately) equal probability.- Specified by:
nextIntin interfaceRandomGenerator- Overrides:
nextIntin classEnhancedRandom- Returns:
- the next pseudorandom, uniformly distributed
intvalue from this random number generator's sequence
-
nextInt
public int nextInt(int bound) Description copied from class:EnhancedRandomReturns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract ofnextIntis that oneintvalue in the specified range is pseudorandomly generated and returned. Allboundpossibleintvalues are produced with (approximately) equal probability.
This method clamps bound to be at least 0; it never returns a negative int.
It should be mentioned that the technique this uses has some bias, depending onbound, but it typically isn't measurable without specifically looking for it. Using the method this does allows this method to always advance the state by one step, instead of a varying and unpredictable amount with the more typical ways of rejection-sampling random numbers and only using numbers that can produce an int within the bound without bias. See M.E. O'Neill's blog about random numbers for discussion of alternative, unbiased methods.- Specified by:
nextIntin interfaceRandomGenerator- Overrides:
nextIntin classEnhancedRandom- Parameters:
bound- the upper bound (exclusive). If negative or 0, this always returns 0.- Returns:
- the next pseudorandom, uniformly distributed
intvalue between zero (inclusive) andbound(exclusive) from this random number generator's sequence
-
nextSignedInt
public int nextSignedInt(int outerBound) Description copied from class:EnhancedRandomReturns a pseudorandom, uniformly distributedintvalue between an inner bound of 0 (inclusive) and the specifiedouterBound(exclusive). This is meant for cases where the outer bound may be negative, especially if the bound is unknown or may be user-specified. A negative outer bound is used as the lower bound; a positive outer bound is used as the upper bound. An outer bound of -1, 0, or 1 will always return 0, keeping the bound exclusive (except for outer bound 0). This method is slightly slower thanEnhancedRandom.nextInt(int).- Overrides:
nextSignedIntin classEnhancedRandom- Parameters:
outerBound- the outer exclusive bound; may be any int value, allowing negative- Returns:
- a pseudorandom int between 0 (inclusive) and outerBound (exclusive)
- See Also:
-
nextBoolean
public boolean nextBoolean()Description copied from class:EnhancedRandomReturns the next pseudorandom, uniformly distributedbooleanvalue from this random number generator's sequence. The general contract ofnextBooleanis that onebooleanvalue is pseudorandomly generated and returned. The valuestrueandfalseare produced with (approximately) equal probability.
The public implementation simply returns a sign check onEnhancedRandom.nextLong(), returning true if the generated long is negative. This is typically the safest way to implement this method; many types of generators have less statistical quality on their lowest bit, so just returning based on the lowest bit isn't always a good idea.- Specified by:
nextBooleanin interfaceRandomGenerator- Overrides:
nextBooleanin classEnhancedRandom- Returns:
- the next pseudorandom, uniformly distributed
booleanvalue from this random number generator's sequence
-
nextGaussian
public double nextGaussian()This runsDistributor.probitD(double)on a distributed double this produces.- Specified by:
nextGaussianin interfaceRandomGenerator- Overrides:
nextGaussianin classEnhancedRandom- Returns:
- a "Gaussian-ized" result of
nextDouble()
-
nextExclusiveDouble
public double nextExclusiveDouble()This does not use the optimizations fromEnhancedRandom.nextExclusiveDouble(), because those aren't reasonable when distributed.- Overrides:
nextExclusiveDoublein classEnhancedRandom- Returns:
- a pseudo-random double between 0.0, exclusive, and 1.0, exclusive
-
nextExclusiveSignedDouble
public double nextExclusiveSignedDouble()This differs from the implementation used by other EnhancedRandom types in that the distribution applies to the(0,1)range as normal, but half the time this will negate that distribution, so it is on(-1,0)and the high and low ends are reversed. For example, if you useDistributedRandom.ReductionMode.REJECTon anExponentialDistribution, which normally has most of its results near 0 and few near 1, then the results will still have most near 0, few near 1, and equally few near -1.- Overrides:
nextExclusiveSignedDoublein classEnhancedRandom- Returns:
- a random uniform double between -1 and 1 with a tiny hole around 0 (all exclusive)
-
nextExclusiveFloat
public float nextExclusiveFloat()This acts the same asEnhancedRandom.nextExclusiveFloatEquidistant(); it does not use the optimizations fromEnhancedRandom.nextExclusiveFloat(), because those aren't reasonable when distributed.- Overrides:
nextExclusiveFloatin classEnhancedRandom- Returns:
- a pseudo-random float between 0.0, exclusive, and 1.0, exclusive
-
nextExclusiveSignedFloat
public float nextExclusiveSignedFloat()This differs from the implementation used by other EnhancedRandom types in that the distribution applies to the(0,1)range as normal, but half the time this will negate that distribution, so it is on(-1,0)and the high and low ends are reversed. For example, if you useDistributedRandom.ReductionMode.REJECTon anExponentialDistribution, which normally has most of its results near 0 and few near 1, then the results will still have most near 0, few near 1, and equally few near -1.- Overrides:
nextExclusiveSignedFloatin classEnhancedRandom- Returns:
- a random uniform float between -1 and 1 with a tiny hole around 0 (all exclusive)
-
copy
Description copied from class:EnhancedRandomCreates 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:
copyin classEnhancedRandom- Returns:
- a deep copy of this EnhancedRandom.
-
setState
public void setState(long stateA) Description copied from class:EnhancedRandomSets each state variable to the givenstate. IfEnhancedRandom.getStateCount()is 1, then this should set the whole state to the given value usingEnhancedRandom.setSelectedState(int, long). If getStateCount() is more than 1, then all states will be set in the same way (using setSelectedState(), all tostate).- Overrides:
setStatein classEnhancedRandom- Parameters:
stateA- the long value to use for each state variable
-
setState
public void setState(long stateA, long stateB) Description copied from class:EnhancedRandomSets each state variable to eitherstateAorstateB, alternating. This usesEnhancedRandom.setSelectedState(int, long)to set the values. If there is one state variable (EnhancedRandom.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:
setStatein 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) Description copied from class:EnhancedRandomSets each state variable tostateA,stateB, orstateC, alternating. This usesEnhancedRandom.setSelectedState(int, long)to set the values. If there is one state variable (EnhancedRandom.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:
setStatein 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) Description copied from class:EnhancedRandomSets each state variable tostateA,stateB,stateC, orstateD, alternating. This usesEnhancedRandom.setSelectedState(int, long)to set the values. If there is one state variable (EnhancedRandom.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:
setStatein 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...
-
getStateCount
public int getStateCount()Description copied from class:EnhancedRandomGets the number of possible state variables that can be selected withEnhancedRandom.getSelectedState(int)orEnhancedRandom.setSelectedState(int, long). This defaults to returning 0, making no state variable available for reading or writing. An implementation that has only onelongstate, likeDistinctRandomgenerator, should return1. A generator that permits setting two differentlongvalues, likeLaserRandom, should return2. Much larger values are possible for types like the Mersenne Twister or some CMWC generators.- Overrides:
getStateCountin classEnhancedRandom- Returns:
- the non-negative number of selections possible for state variables
-
getSelectedState
public long getSelectedState(int selection) Description copied from class:EnhancedRandomGets a selected state value from this EnhancedRandom. The number of possible selections is up to the implementing class, and is accessible viaEnhancedRandom.getStateCount(), but negative values forselectionare 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,EnhancedRandom.getStateCount()must also be implemented.- Overrides:
getSelectedStatein 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) Description copied from class:EnhancedRandomSets a selected state value to the given longvalue. The number of possible selections is up to the implementing class, but negative values forselectionare typically not tolerated. Implementors are permitted to changevalueif it is not valid, but they should not alter it if it is valid. The public implementation callsEnhancedRandom.setSeed(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 whenEnhancedRandom.getSelectedState(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:
setSelectedStatein 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
-
setSeed
public void setSeed(long seed) Description copied from class:EnhancedRandomSets the seed of this random number generator using a singlelongseed. This should behave exactly the same as if a new object of this type was created with the constructor that takes a singlelongvalue. This does not necessarily assign the state variable(s) of the implementation with the exact contents of seed, soEnhancedRandom.getSelectedState(int)should not be expected to returnseedafter this, though it may. If this implementation has more than onelongof state, then the expectation is that none of those state variables will be exactly equal toseed(almost all the time).- Specified by:
setSeedin classEnhancedRandom- Parameters:
seed- the initial seed
-
getDistribution
-
setDistribution
-
getRandom
-
setRandom
-
stringSerialize
Description copied from class:EnhancedRandomSerializes the current state of this EnhancedRandom to a String that can be used byEnhancedRandom.stringDeserialize(String)to load this state at another time. May use anyBase;Base.BASE10andBase.BASE16are the most intuitive, butBase.SIMPLE64and especiallyBase.BASE90will be more compact.- Overrides:
stringSerializein classEnhancedRandom- Parameters:
base- which Base to use, from the "digital" library, such asBase.BASE10- Returns:
- this, for chaining
-
appendSerialized
public <T extends CharSequence & Appendable> T appendSerialized(T sb, com.github.tommyettinger.digital.Base base) Description copied from class:EnhancedRandomSerializes the current state of this EnhancedRandom and appends it to an Appendable CharSequence (such as a StringBuilder), which may be used byEnhancedRandom.stringDeserialize(String)to load this state at another time. May use anyBase;Base.BASE10andBase.BASE16are the most intuitive, butBase.SIMPLE64and especiallyBase.BASE90will be more compact.- Overrides:
appendSerializedin classEnhancedRandom- Type Parameters:
T- any type that is both a CharSequence and an Appendable, such as StringBuilder, StringBuffer, or CharBuffer- Parameters:
sb- an Appendable CharSequence that will be modifiedbase- which Base to use, from the "digital" library, such asBase.BASE10- Returns:
sb, for chaining
-
stringDeserialize
Description copied from class:EnhancedRandomGiven a String in the format produced byEnhancedRandom.stringSerialize(Base), and the sameBaseused 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:
stringDeserializein classEnhancedRandom- Parameters:
data- a String probably produced bystringSerialize(Base)base- which Base to use, from the "digital" library, such asBase.BASE10- Returns:
- this, for chaining
- See Also:
-
writeExternal
Needs the type ofdistributionregistered.- Specified by:
writeExternalin interfaceExternalizable- Overrides:
writeExternalin 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:
readExternalin interfaceExternalizable- Overrides:
readExternalin classEnhancedRandom- Parameters:
in- the stream to read data from in order to restore the object- Throws:
IOException- if I/O errors occurClassNotFoundException
-
toString
-