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
Modifier and TypeFieldDescriptionlong
The secondary state of the generator; the upper 6 bits are used to determine which single bit will change instate
when a new number is generated.long
The primary state of the generator; this is what gets returned bynextLong()
. -
Constructor Summary
ConstructorDescriptionLowChangeQuasiRandom
(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.boolean
long
getSelectedState
(int selection) Gets a selected state value from this EnhancedRandom.int
Gets 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.long
nextLong()
Returns the next pseudorandom, uniformly distributedlong
value from this random number generator's sequence.long
Optional; moves the state to its previous value and returns the previous long that would have been produced byEnhancedRandom.nextLong()
.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 stateA, long stateB) Sets each state variable to eitherstateA
orstateB
, alternating.toString()
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, readExternal, seedFromMath, 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, writeExternal
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, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.random.RandomGenerator
isDeprecated, nextExponential
-
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 instate
when 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:EnhancedRandom
Gets 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 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) Description copied from class:EnhancedRandom
Gets 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 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,EnhancedRandom.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) Description copied from class:EnhancedRandom
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 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:
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) Description copied from class:EnhancedRandom
Sets 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:
setState
in classEnhancedRandom
- Parameters:
state
- the long value to use for each state variable
-
setState
public void setState(long stateA, long stateB) Description copied from class:EnhancedRandom
Sets each state variable to eitherstateA
orstateB
, 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:
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...
-
getTag
Description copied from class:EnhancedRandom
Gets 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:
getTag
in classEnhancedRandom
- Returns:
- a unique String identifier for this type of EnhancedRandom; usually 4 chars long.
-
setSeed
public void setSeed(long seed) Description copied from class:EnhancedRandom
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, soEnhancedRandom.getSelectedState(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()Description copied from class:EnhancedRandom
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 andEnhancedRandom.copy()
, 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
-
previousLong
public long previousLong()Description copied from class:EnhancedRandom
Optional; 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 generateint
results typically producelong
values 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 producelong
values usually don't need to implementEnhancedRandom.previousInt()
, but those that produceint
usually 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:
previousLong
in classEnhancedRandom
- Returns:
- the previous number this would have produced with
EnhancedRandom.nextLong()
-
copy
Description copied from class:EnhancedRandom
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.
-
equals
-
toString
-