Class TupleQuasiRandom
- All Implemented Interfaces:
Externalizable
,Serializable
,RandomGenerator
GoldenQuasiRandom
. GoldenQuasiRandom doesn't produce actually quasi-random numbers when generating Gaussian
values; instead it produces pseudo-random numbers using an algorithm like DistinctRandom
. If
GoldenQuasiRandom did use its normal algorithm, it would cause severe artifacts when a tuple of Gaussian values was
obtained -- while this class is similar in many ways to GoldenQuasiRandom, it doesn't have this tuple issue, and it
does still produce quasi-random Gaussian values, more or less. This has a period of 2 to the 64.
It does not pass any tests for randomness. This is simply a counter with an increment of 1, that
uses the counter times one of 1024 possible multipliers, which this cycles through. The multipliers are the first
1024 items in MathTools.GOLDEN_LONGS
.
Useful traits of this generator are that all values are permitted for the main state, that it converges quickly when retrieving tuples of normal-distributed numbers, and that you can
skip(long)
the state forwards or backwards
in constant time.
This class is an
EnhancedRandom
from juniper and is also a JDK Random
as a result.
This doesn't randomize the seed when given one with
setSeed(long)
, and it doesn't do anything else to
randomize the output, so sequential seeds will produce extremely similar sequences. You can randomize sequential
seeds using something like Hasher.randomize3(long)
, if you want random starting points.
This implements all methods from
EnhancedRandom
, including the optional skip(long)
and
previousLong()
methods.- 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
ConstructorDescriptionCreates a new GoldenQuasiRandom with a random state.TupleQuasiRandom
(long state) Creates a new GoldenQuasiRandom with the given state; alllong
values are permitted. -
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) This gets the main state's exact value, ignoring selection.long
getState()
Gets the current state; it's already public, but I guess this could still be useful.int
This has one long state.getTag()
Gets the tag used to identify this type of EnhancedRandom, as a String.int
next
(int bits) Generates the next pseudorandom number with a specific maximum size in bits (not a max number).double
Gets a random double between 0.0 and 1.0, exclusive at both ends; this method is also more uniform thanEnhancedRandom.nextDouble()
if you use the bit-patterns of the returned doubles.float
Gets a random float between 0.0 and 1.0, exclusive at both ends.double
Gets a random double that may be positive or negative, but cannot be 0, and always has a magnitude less than 1.float
Gets a random float that may be positive or negative, but cannot be 0, and always has a magnitude less than 1.double
Returns the next pseudorandom, Gaussian ("normally") distributeddouble
value with mean0.0
and standard deviation1.0
from this random number generator's sequence.int
nextInt()
Returns the next pseudorandom, uniformly distributedint
value from this random number generator's sequence.int
nextInt
(int bound) Returns a pseudorandom, uniformly distributedint
value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.long
nextLong()
Returns the next pseudorandom, uniformly distributedlong
value from this random number generator's sequence.int
nextSignedInt
(int outerBound) Returns a pseudorandom, uniformly distributedint
value between an inner bound of 0 (inclusive) and the specifiedouterBound
(exclusive).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 only state, which can be given any long value; this seed value will not be altered.void
setSelectedState
(int selection, long value) This always sets the main state, which can be any long value.void
setState
(long state) Sets the main state variable to the givenstate
.long
skip
(long advance) Skips the state forward or backwards by the givenadvance
, then returns the result ofnextLong()
at the same point in the sequence.toString()
Methods inherited from class com.github.tommyettinger.random.EnhancedRandom
areEqual, fixGamma, maxDoubleOf, maxFloatOf, maxIntOf, maxLongOf, minDoubleOf, minFloatOf, minIntOf, minLongOf, nextBoolean, nextBoolean, nextBytes, nextDouble, nextDouble, nextDouble, nextExclusiveDouble, nextExclusiveDouble, nextExclusiveDoubleEquidistant, nextExclusiveFloat, nextExclusiveFloat, nextExclusiveFloatEquidistant, nextFloat, nextFloat, nextFloat, nextGaussian, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveDouble, nextInclusiveFloat, nextInclusiveFloat, nextInclusiveFloat, nextInt, nextLong, nextLong, nextSign, nextSignedInt, nextSignedLong, nextSignedLong, nextTriangular, nextTriangular, nextTriangular, nextTriangular, nextUnsignedInt, previousInt, probit, randomElement, randomElement, 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, 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 main state variable, as a long; can be anylong
.
-
-
Constructor Details
-
TupleQuasiRandom
public TupleQuasiRandom()Creates a new GoldenQuasiRandom with a random state. -
TupleQuasiRandom
public TupleQuasiRandom(long state) Creates a new GoldenQuasiRandom with the given state; alllong
values are permitted.- Parameters:
state
- anylong
value
-
-
Method Details
-
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.
-
getStateCount
public int getStateCount()This has one long state.- Overrides:
getStateCount
in classEnhancedRandom
- Returns:
- 1 (one)
-
getSelectedState
public long getSelectedState(int selection) This gets the main state's exact value, ignoring selection.- Overrides:
getSelectedState
in classEnhancedRandom
- Parameters:
selection
- ignored- Returns:
- the main state's exact value
-
setSelectedState
public void setSelectedState(int selection, long value) This always sets the main state, which can be any long value.- Overrides:
setSelectedState
in classEnhancedRandom
- Parameters:
selection
- ignoredvalue
- the exact value to use for the main state; all longs are valid for the main state
-
setSeed
public void setSeed(long seed) Sets the only state, which can be given any long value; this seed value will not be altered. Equivalent tosetSelectedState(int, long)
with any selection andseed
passed as thevalue
.- Specified by:
setSeed
in classEnhancedRandom
- Parameters:
seed
- the exact value to use for the state; all longs are valid
-
getState
public long getState()Gets the current state; it's already public, but I guess this could still be useful. The state can be anylong
.- Returns:
- the current state, as a long
-
setState
public void setState(long state) Sets the main state variable to the givenstate
.- Overrides:
setState
in classEnhancedRandom
- Parameters:
state
- the long value to use for the state variable
-
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
-
skip
public long skip(long advance) Skips the state forward or backwards by the givenadvance
, then returns the result ofnextLong()
at the same point in the sequence. If advance is 1, this is equivalent to nextLong(). If advance is 0, this returns the samelong
as the previous call to the generator (if it called nextLong()), and doesn't change the state. If advance is -1, this moves the state backwards and produces thelong
before the last one generated by nextLong(). More positive numbers move the state further ahead, and more negative numbers move the state further behind; all of these take constant time.- Overrides:
skip
in classEnhancedRandom
- Parameters:
advance
- how many steps to advance the state before generating along
- Returns:
- a random
long
by the same algorithm asnextLong()
, using the appropriately-advanced state
-
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()
-
next
public int next(int bits) Description copied from class:EnhancedRandom
Generates 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)
. Forbits
values 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. Ifbits
is 31, this can return any non-negativeint
; note thatnextInt(1 << 31)
won't behave this way because1 << 31
is negative. Ifbits
is 32 (or 0), this can return anyint
.The general contract of
next
is that it returns anint
value and if the argumentbits
is between1
and32
(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 be0
or1
.Note that you can give this values for
bits
that 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:
next
in 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
-
nextInt
public int nextInt()Description copied from class:EnhancedRandom
Returns the next pseudorandom, uniformly distributedint
value from this random number generator's sequence. The general contract ofnextInt
is that oneint
value is pseudorandomly generated and returned. All 232 possibleint
values are produced with (approximately) equal probability.- Specified by:
nextInt
in interfaceRandomGenerator
- Overrides:
nextInt
in classEnhancedRandom
- Returns:
- the next pseudorandom, uniformly distributed
int
value from this random number generator's sequence
-
nextInt
public int nextInt(int bound) Description copied from class:EnhancedRandom
Returns a pseudorandom, uniformly distributedint
value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract ofnextInt
is that oneint
value in the specified range is pseudorandomly generated and returned. Allbound
possibleint
values 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:
nextInt
in interfaceRandomGenerator
- Overrides:
nextInt
in classEnhancedRandom
- Parameters:
bound
- the upper bound (exclusive). If negative or 0, this always returns 0.- Returns:
- the next pseudorandom, uniformly distributed
int
value between zero (inclusive) andbound
(exclusive) from this random number generator's sequence
-
nextSignedInt
public int nextSignedInt(int outerBound) Description copied from class:EnhancedRandom
Returns a pseudorandom, uniformly distributedint
value 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:
nextSignedInt
in 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:
-
nextExclusiveDouble
public double nextExclusiveDouble()Description copied from class:EnhancedRandom
Gets a random double between 0.0 and 1.0, exclusive at both ends; this method is also more uniform thanEnhancedRandom.nextDouble()
if you use the bit-patterns of the returned doubles. This is a simplified version of this algorithm by Allen Downey. This can return double values between 2.710505431213761E-20 and 0.9999999999999999, or 0x1.0p-65 and 0x1.fffffffffffffp-1 in hex notation. It cannot return 0 or 1. Some cases can preferEnhancedRandom.nextExclusiveDoubleEquidistant()
, which is implemented more traditionally but may have slower performance. This method can also return doubles that are extremely close to 0, but can't return doubles that are as close to 1, due to how floating-point numbers work. However, nextExclusiveDoubleEquidistant() can return only a minimum value that is as distant from 0 as its maximum value is distant from 1.
To compare, nextDouble() and nextExclusiveDoubleEquidistant() are less likely to produce a "1" bit for their lowest 5 bits of mantissa/significand (the least significant bits numerically, but potentially important for some uses), with the least significant bit produced half as often as the most significant bit in the mantissa. As for this method, it has approximately the same likelihood of producing a "1" bit for any position in the mantissa.
The implementation may have different performance characteristics thanEnhancedRandom.nextDouble()
, because this doesn't perform any floating-point multiplication or division, and instead assembles bits obtained by one call toEnhancedRandom.nextLong()
. This usesBitConversion.longBitsToDouble(long)
andBitConversion.countTrailingZeros(long)
, both of which typically have optimized intrinsics on HotSpot, and this is branchless and loopless, unlike the original algorithm by Allen Downey. When compared withEnhancedRandom.nextExclusiveDoubleEquidistant()
, this method performs better on at least HotSpot JVMs. On GraalVM 17, this is over twice as fast as nextExclusiveDoubleEquidistant().- Overrides:
nextExclusiveDouble
in classEnhancedRandom
- Returns:
- a random uniform double between 2.710505431213761E-20 and 0.9999999999999999 (both inclusive)
-
nextExclusiveSignedDouble
public double nextExclusiveSignedDouble()Description copied from class:EnhancedRandom
Gets a random double that may be positive or negative, but cannot be 0, and always has a magnitude less than 1.
This is a modified version of this algorithm by Allen Downey. This version can return double values between -0.9999999999999999 and -5.421010862427522E-20, as well as between 2.710505431213761E-20 and 0.9999999999999999, or -0x1.fffffffffffffp-1 to -0x1.0p-64 as well as between 0x1.0p-65 and 0x1.fffffffffffffp-1 in hex notation. It cannot return -1, 0 or 1. It has much more uniform bit distribution across its mantissa/significand bits thanRandom.nextDouble()
, especially when the result of nextDouble() is expanded to the -1.0 to 1.0 range (such as with2.0 * (nextDouble() - 0.5)
). Where the given example code is unable to produce a "1" bit for its lowest bit of mantissa (the least significant bits numerically, but potentially important for some uses), this has approximately the same likelihood of producing a "1" bit for any positions in the mantissa, and also equal odds for the sign bit.- Overrides:
nextExclusiveSignedDouble
in classEnhancedRandom
- Returns:
- a random uniform double between -1 and 1 with a tiny hole around 0 (all exclusive)
-
nextExclusiveFloat
public float nextExclusiveFloat()Description copied from class:EnhancedRandom
Gets a random float between 0.0 and 1.0, exclusive at both ends. This method is also more uniform thanEnhancedRandom.nextFloat()
if you use the bit-patterns of the returned floats. This is a simplified version of this algorithm by Allen Downey. This version can return float values between 2.7105054E-20 to 0.99999994, or 0x1.0p-65 to 0x1.fffffep-1 in hex notation. It cannot return 0 or 1. To compare, nextFloat() is less likely to produce a "1" bit for its lowest 5 bits of mantissa/significand (the least significant bits numerically, but potentially important for some uses), with the least significant bit produced half as often as the most significant bit in the mantissa. As for this method, it has approximately the same likelihood of producing a "1" bit for any position in the mantissa.
The implementation may have different performance characteristics thanEnhancedRandom.nextFloat()
, because this doesn't perform any floating-point multiplication or division, and instead assembles bits obtained by one call toEnhancedRandom.nextLong()
. This usesBitConversion.intBitsToFloat(int)
andBitConversion.countLeadingZeros(long)
, both of which typically have optimized intrinsics on HotSpot, and this is branchless and loopless, unlike the original algorithm by Allen Downey. When compared withEnhancedRandom.nextExclusiveFloatEquidistant()
, this method performs better on at least HotSpot JVMs. On GraalVM 17, this is over twice as fast as nextExclusiveFloatEquidistant().- Overrides:
nextExclusiveFloat
in classEnhancedRandom
- Returns:
- a random uniform float between 0 and 1 (both exclusive)
-
nextExclusiveSignedFloat
public float nextExclusiveSignedFloat()Description copied from class:EnhancedRandom
Gets a random float that may be positive or negative, but cannot be 0, and always has a magnitude less than 1.
This is a modified version of this algorithm by Allen Downey. This version can return double values between -0.99999994 and -1.1641532E-10, as well as between 2.7105054E-20 and 0.99999994, or -0x1.fffffep-1 to -0x1.0p-33 as well as between 0x1.0p-65 and 0x1.fffffep-1 in hex notation. It cannot return -1, 0 or 1. It has much more uniform bit distribution across its mantissa/significand bits thanRandom.nextDouble()
, especially when the result of nextDouble() is expanded to the -1.0 to 1.0 range (such as with2.0 * (nextDouble() - 0.5)
). Where the given example code is unable to produce a "1" bit for its lowest bit of mantissa (the least significant bits numerically, but potentially important for some uses), this has approximately the same likelihood of producing a "1" bit for any positions in the mantissa, and also equal odds for the sign bit.- Overrides:
nextExclusiveSignedFloat
in classEnhancedRandom
- Returns:
- a random uniform double between -1 and 1 with a tiny hole around 0 (all exclusive)
-
nextGaussian
public double nextGaussian()Description copied from class:EnhancedRandom
Returns the next pseudorandom, Gaussian ("normally") distributeddouble
value with mean0.0
and standard deviation1.0
from this random number generator's sequence.The general contract of
nextGaussian
is that onedouble
value, chosen from (approximately) the usual normal distribution with mean0.0
and standard deviation1.0
, is pseudorandomly generated and returned.This does not use a rough approximation, which is a departure from earlier versions; instead, it uses the Ziggurat method, which produces high-quality variables very quickly. Like earlier versions that used probit() or a bit-counting approximation, this requests exactly one long from the generator's sequence (using
EnhancedRandom.nextLong()
). This makes it different from code like java.util.Random's nextGaussian() method, which can (rarely) fetch a higher number of random doubles.The implementation here was ported from code by Olaf Berstein, based on a paper by Jorgen A. Doornik and some steps from a paper by George Marsaglia.
Ziggurat
has more information, for the curious.- Specified by:
nextGaussian
in interfaceRandomGenerator
- Overrides:
nextGaussian
in classEnhancedRandom
- Returns:
- the next pseudorandom, Gaussian ("normally") distributed
double
value with mean0.0
and standard deviation1.0
from this random number generator's sequence
-
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
-