Package com.github.tommyettinger.digital
Class AlternateRandom
java.lang.Object
java.util.Random
com.github.tommyettinger.digital.AlternateRandom
- All Implemented Interfaces:
Serializable
,RandomGenerator
A drop-in replacement for
This is mostly here to speed up the default shuffling methods in
Random
that adds no new APIs, but is faster, has better statistical quality, and
has a guaranteed longer minimum period (also called cycle length). This is similar to PasarRandom in the Juniper
library, and uses the same algorithm, but only extends Random, not Juniper's EnhancedRandom class. If you depend on
Juniper, you lose nothing from using the EnhancedRandom classes (they also extend Random), but this class doesn't
have as many features as Juniper's PasarRandom. AlternateRandom doesn't depend on anything outside the JDK, though,
so it fits easily as a small addition into digital.
This is mostly here to speed up the default shuffling methods in
ArrayTools
, and to allow those to produce
many more possible shuffles. The number of possible shuffles that can be produced by that type of algorithm depends
on the number of possible states of the random number generator; that number here is 2 to the 320, or
2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936576 (but the actual
amount in practice is smaller, to a hard minimum of 2 to the 64, or 18446744073709551616).- 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
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new AlternateRandom with a random state.AlternateRandom
(long seed) Creates a new AlternateRandom with the given seed; alllong
values are permitted. -
Method Summary
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, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.random.RandomGenerator
isDeprecated, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextGaussian, nextInt, nextLong, nextLong
-
Constructor Details
-
AlternateRandom
public AlternateRandom()Creates a new AlternateRandom with a random state. -
AlternateRandom
public AlternateRandom(long seed) Creates a new AlternateRandom with the given seed; alllong
values are permitted. The seed will be passed tosetSeed(long)
to attempt to adequately distribute the seed randomly.- Parameters:
seed
- anylong
value
-
-
Method Details
-
setSeed
public void setSeed(long seed) This initializes all 5 states of the generator to random values based on the given seed. (2 to the 64) possible initial generator states can be produced here. -
nextLong
public long nextLong()- Specified by:
nextLong
in interfaceRandomGenerator
- Overrides:
nextLong
in classRandom
-
next
public int next(int bits) -
nextInt
public int nextInt()- Specified by:
nextInt
in interfaceRandomGenerator
- Overrides:
nextInt
in classRandom
-
nextInt
public int nextInt(int bound) - Specified by:
nextInt
in interfaceRandomGenerator
- Overrides:
nextInt
in classRandom
-
nextBoolean
public boolean nextBoolean()- Specified by:
nextBoolean
in interfaceRandomGenerator
- Overrides:
nextBoolean
in classRandom
-
nextFloat
public float nextFloat()- Specified by:
nextFloat
in interfaceRandomGenerator
- Overrides:
nextFloat
in classRandom
-
nextDouble
public double nextDouble()- Specified by:
nextDouble
in interfaceRandomGenerator
- Overrides:
nextDouble
in classRandom
-
nextGaussian
public double nextGaussian()- Specified by:
nextGaussian
in interfaceRandomGenerator
- Overrides:
nextGaussian
in classRandom
-
nextBytes
public void nextBytes(byte[] bytes) - Specified by:
nextBytes
in interfaceRandomGenerator
- Overrides:
nextBytes
in classRandom
-