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
This does have some small additions to what a Random can provide: There is a constructor that takes all five states verbatim, the states themselves are all public longs, there is a
Random
that adds few 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).
This does have some small additions to what a Random can provide: There is a constructor that takes all five states verbatim, the states themselves are all public longs, there is a
copy()
method that does what it says, and
there are serializeToString()
and deserializeFromString(String)
methods to read and write the state
using Base.SIMPLE64
. The serialization format is very terse, just 11 chars per state, in A through E order,
appended one after the next without spaces. This format is not at all compatible with Juniper. Having all this allows
AlternateRandom to be correctly handled by jdkgdxds-interop for libGDX Json serialization, and kryo-more for Kryo.- 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
Fields -
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.AlternateRandom
(long a, long b, long c, long d, long e) Creates a new AlternateRandom with the given five states; alllong
values are permitted. -
Method Summary
Modifier and TypeMethodDescriptioncopy()
deserializeFromString
(String data) Given a String produced byserializeToString()
, this sets the state of this AlternateRandom to the state stored in that String.This does not deserialize any fields inherited fromRandom
, so the methods that use Random's side entirely, such as the Stream methods, won't be affected by this state.int
next
(int bits) boolean
void
nextBytes
(byte[] bytes) double
float
double
int
nextInt()
int
nextInt
(int bound) long
nextLong()
Produces a String that holds the entire state of this AlternateRandom.void
setSeed
(long seed) This initializes all 5 states of the generator to random values based on the given seed.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
-
Field Details
-
stateA
public long stateAThe first state; can be any long. -
stateB
public long stateBThe second state; can be any long. -
stateC
public long stateCThe third state; can be any long. -
stateD
public long stateDThe fourth state; can be any long. This state is the counter, and it is not affected by the other states. -
stateE
public long stateEThe fifth state; can be any long.
-
-
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
-
AlternateRandom
public AlternateRandom(long a, long b, long c, long d, long e) Creates a new AlternateRandom with the given five states; alllong
values are permitted. The states will be not be changed at all, which can be useful to reproduce problematic conditions.- Parameters:
a
- anylong
valueb
- anylong
valuec
- anylong
valued
- anylong
valuee
- 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
-
serializeToString
Produces a String that holds the entire state of this AlternateRandom. You can recover this state from such a String by callingdeserializeFromString(String)
on any AlternateRandom, which will set that AlternateRandom's state. This does not serialize any fields inherited fromRandom
, so the methods that use Random's side entirely, such as the Stream methods, won't be affected if this state is loaded.- Returns:
- a String holding the current state of this AlternateRandom, to be loaded by
deserializeFromString(String)
-
deserializeFromString
Given a String produced byserializeToString()
, this sets the state of this AlternateRandom to the state stored in that String.This does not deserialize any fields inherited fromRandom
, so the methods that use Random's side entirely, such as the Stream methods, won't be affected by this state.- Parameters:
data
- a String produced byserializeToString()
- Returns:
- this AlternateRandom, after its state has been loaded from the given String
-
copy
-