Package squidpony.squidmath
Class StatefulRNG
java.lang.Object
squidpony.squidmath.RNG
squidpony.squidmath.StatefulRNG
- All Implemented Interfaces:
Serializable
,IRNG
,IStatefulRNG
,RandomnessSource
,StatefulRandomness
public class StatefulRNG extends RNG implements Serializable, IStatefulRNG
A slight variant on RNG that always uses a stateful RandomessSource and so can have its state
set or retrieved using setState() or getState().
Created by Tommy Ettinger on 9/15/2015.
- Author:
- Tommy Ettinger
- See Also:
- Serialized Form
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
Constructors Constructor Description StatefulRNG()
StatefulRNG(long seed)
Seeded constructor uses DiverRNG, which is of high quality, but low period (which rarely matters for games), and has good speed and tiny state size.StatefulRNG(CharSequence seedString)
String-seeded constructor uses the hash of the String as a seed for DiverRNG, which is of high quality, but low period (which rarely matters for games), and has good speed and tiny state size.StatefulRNG(RandomnessSource random)
-
Method Summary
Modifier and Type Method Description StatefulRNG
copy()
Creates a copy of this StatefulRNG; it will generate the same random numbers, given the same calls in order, as this StatefulRNG at the point copy() is called.long
getState()
Get a long that can be used to reproduce the sequence of random numbers this object will generate starting now.void
setRandomness(RandomnessSource random)
void
setState(long state)
Sets the state of the random number generator to a given long, which will alter future random numbers this produces based on the state.Serializable
toSerializable()
Returns this StatefulRNG in a way that can be deserialized even if onlyIRNG
's methods can be called.String
toString()
Methods inherited from class squidpony.squidmath.RNG
approximateBits, asRandom, between, between, between, betweenWeighted, equals, getRandomCellsIterable, getRandomElement, getRandomElement, getRandomElement, getRandomness, getRandomStartIterable, getRandomUniqueCells, getRandomUniqueCells, getRandomUniqueCells, hashCode, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, next, nextBoolean, nextBytes, nextCoord, nextCurvedFloat, nextDouble, nextDouble, nextDoubleInclusive, nextDoubleInclusive, nextFloat, nextFloat, nextFloatInclusive, nextFloatInclusive, nextInt, nextInt, nextIntHasty, nextLong, nextLong, nextSignedInt, nextSignedLong, randomInterleave, randomOrdering, randomOrdering, randomPortion, randomPortion, randomRange, randomRotation, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface squidpony.squidmath.IRNG
between, between, between, getRandomElement, getRandomElement, getRandomElement, next, nextBoolean, nextDouble, nextDouble, nextFloat, nextFloat, nextInt, nextInt, nextLong, nextLong, nextSignedInt, nextSignedLong, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
-
Constructor Details
-
StatefulRNG
public StatefulRNG() -
StatefulRNG
-
StatefulRNG
Seeded constructor uses DiverRNG, which is of high quality, but low period (which rarely matters for games), and has good speed and tiny state size. -
StatefulRNG
String-seeded constructor uses the hash of the String as a seed for DiverRNG, which is of high quality, but low period (which rarely matters for games), and has good speed and tiny state size. Note: This constructor changed behavior on April 22, 2017, again on December 23, 2017, and again on June 14, 2018. The first was when it was noticed that it was not seeding very effectively (only assigning to 32 bits of seed instead of all 64). The older behavior isn't fully preserved, since it used a rather low-quality String hashing algorithm and so probably had problems producing good starting seeds, but you can get close by replacingnew StatefulRNG(text)
withnew StatefulRNG(new LightRNG(CrossHash.hash(text)))
. The new technique assigns to all 64 bits and has less correlation between similar inputs causing similar starting states. It's also faster, but that shouldn't matter in a constructor. It uses a better hashing algorithm because CrossHash no longer has the older, worse one. The latest change in June switched to DiverRNG instead of LightRNG.
-
-
Method Details
-
setRandomness
- Overrides:
setRandomness
in classRNG
-
copy
Creates a copy of this StatefulRNG; it will generate the same random numbers, given the same calls in order, as this StatefulRNG at the point copy() is called. The copy will not share references with this StatefulRNG.- Specified by:
copy
in interfaceIRNG
- Specified by:
copy
in interfaceRandomnessSource
- Specified by:
copy
in interfaceStatefulRandomness
- Overrides:
copy
in classRNG
- Returns:
- a copy of this StatefulRNG
-
getState
Get a long that can be used to reproduce the sequence of random numbers this object will generate starting now.- Specified by:
getState
in interfaceStatefulRandomness
- Returns:
- a long that can be used as state.
-
setState
Sets the state of the random number generator to a given long, which will alter future random numbers this produces based on the state.- Specified by:
setState
in interfaceStatefulRandomness
- Parameters:
state
- a long, which typically should not be 0 (some implementations may tolerate a state of 0, however).
-
toString
-
toSerializable
Returns this StatefulRNG in a way that can be deserialized even if onlyIRNG
's methods can be called.- Specified by:
toSerializable
in interfaceIRNG
- Overrides:
toSerializable
in classRNG
- Returns:
- a
Serializable
view of this StatefulRNG; alwaysthis
-