Package com.github.tommyettinger.random
Class LowChangeQuasiRandom
java.lang.Object
java.util.Random
com.github.tommyettinger.random.EnhancedRandom
com.github.tommyettinger.random.LowChangeQuasiRandom
- All Implemented Interfaces:
Externalizable,Serializable,RandomGenerator
A quasi-random number generator that only changes one bit in its
There's probably some good usage for this, but I don't know what it is yet. This generator may be useful simply because it is so non-random, while still being not-quite-predictable. As an example, it changes from returning a negative number many times in a row, to returning a positive number many times in a row (or vice versa), on approximately 1/64 numbers generated. A fair uniform random number generator would change from negative to positive or vice versa on approximately 1/2 numbers generated.
This has a period of 2 to the 64, and allows all
state per call to nextLong(), and
returns that changed state directly. The choice of which bit changes is determined by choice, which itself
changes using the same algorithm as GoldenQuasiRandom. That is, choice has
0x9E3779B97F4A7C15L added to it every time a number is generated, and here the top 6 bits are used to choose
a bit to change in state.
There's probably some good usage for this, but I don't know what it is yet. This generator may be useful simply because it is so non-random, while still being not-quite-predictable. As an example, it changes from returning a negative number many times in a row, to returning a positive number many times in a row (or vice versa), on approximately 1/64 numbers generated. A fair uniform random number generator would change from negative to positive or vice versa on approximately 1/2 numbers generated.
This has a period of 2 to the 64, and allows all
long values for state and for choice. This
means there are 2 to the 64 different streams for this generator, though many are probably very similar.- 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
FieldsModifier and TypeFieldDescriptionlongThe secondary state of the generator; the upper 6 bits are used to determine which single bit will change instatewhen a new number is generated.longThe primary state of the generator; this is what gets returned bynextLong(). -
Constructor Summary
ConstructorsConstructorDescriptionLowChangeQuasiRandom(long seed) LowChangeQuasiRandom(long state, long choice) -
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.boolean2 to the 64.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.longnextLong()Returns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence.longOptional; moves the state to its previous value and returns the previous long that would have been produced byEnhancedRandom.nextLong().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 state) Sets each state variable to the givenstate.voidsetState(long stateA, long stateB) Sets each state variable to eitherstateAorstateB, alternating.toString()Methods inherited from class com.github.tommyettinger.random.EnhancedRandom
appendSerialized, appendSerialized, areEqual, fixGamma, fixGamma, lcm, mainlyGeneratesInt, 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, nextExponential, 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, rateGamma, readExternal, 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, stringDeserialize, stringSerialize, stringSerialize, writeExternalMethods 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, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.random.RandomGenerator
isDeprecated
-
Field Details
-
state
public long stateThe primary state of the generator; this is what gets returned bynextLong(). -
choice
public long choiceThe secondary state of the generator; the upper 6 bits are used to determine which single bit will change instatewhen a new number is generated.
-
-
Constructor Details
-
LowChangeQuasiRandom
public LowChangeQuasiRandom() -
LowChangeQuasiRandom
public LowChangeQuasiRandom(long seed) -
LowChangeQuasiRandom
public LowChangeQuasiRandom(long state, long choice)
-
-
Method Details
-
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
-
setState
public void setState(long state) 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:
state- 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...
-
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.
-
getMinimumPeriod
2 to the 64.- Overrides:
getMinimumPeriodin classEnhancedRandom- Returns:
- 2 to the 64
-
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
-
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
-
previousLong
public long previousLong()Description copied from class:EnhancedRandomOptional; moves the state to its previous value and returns the previous long that would have been produced byEnhancedRandom.nextLong(). This can be equivalent to callingEnhancedRandom.skip(long)with -1L, but not always; many generators can't efficiently skip long distances, but can step back by one value.
Generators that natively generateintresults typically producelongvalues by generating an int for the high 32 bits and an int for the low 32 bits. When producing the previous long, the order the high and low bits are generated, such as byEnhancedRandom.previousInt(), should be reversed. Generators that natively producelongvalues usually don't need to implementEnhancedRandom.previousInt(), but those that produceintusually should implement it, and may optionally call previousInt() twice in this method.
If you know how to implement the reverse of a particular random number generator, it is recommended you do so here, rather than rely on skip(). This isn't always easy, but should always be possible for any decent PRNG (some historical PRNGs, such as the Middle-Square PRNG, cannot be reversed at all). If a generator cannot be reversed because multiple initial states can transition to the same subsequent state, it is known to have statistical problems that are not necessarily present in a generator that matches one initial state to one subsequent state.
The public implementation callsEnhancedRandom.skip(long)with -1L, and if skip() has not been implemented differently, then it will throw an UnsupportedOperationException.- Overrides:
previousLongin classEnhancedRandom- Returns:
- the previous number this would have produced with
EnhancedRandom.nextLong()
-
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.
-
equals
-
toString
-