Class AlternateRandom

java.lang.Object
java.util.Random
com.github.tommyettinger.digital.AlternateRandom
All Implemented Interfaces:
Serializable, RandomGenerator

public class AlternateRandom extends Random
A drop-in replacement for 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:
  • Field Details

    • stateA

      public long stateA
      The first state; can be any long.
    • stateB

      public long stateB
      The second state; can be any long.
    • stateC

      public long stateC
      The third state; can be any long.
    • stateD

      public long stateD
      The fourth state; can be any long. This state is the counter, and it is not affected by the other states.
    • stateE

      public long stateE
      The 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; all long values are permitted. The seed will be passed to setSeed(long) to attempt to adequately distribute the seed randomly.
      Parameters:
      seed - any long value
    • AlternateRandom

      public AlternateRandom(long a, long b, long c, long d, long e)
      Creates a new AlternateRandom with the given five states; all long values are permitted. The states will be not be changed at all, which can be useful to reproduce problematic conditions.
      Parameters:
      a - any long value
      b - any long value
      c - any long value
      d - any long value
      e - any long 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.
      Overrides:
      setSeed in class Random
      Parameters:
      seed - the initial seed; may be any long
    • nextLong

      public long nextLong()
      Specified by:
      nextLong in interface RandomGenerator
      Overrides:
      nextLong in class Random
    • next

      public int next(int bits)
      Overrides:
      next in class Random
    • nextInt

      public int nextInt()
      Specified by:
      nextInt in interface RandomGenerator
      Overrides:
      nextInt in class Random
    • nextInt

      public int nextInt(int bound)
      Specified by:
      nextInt in interface RandomGenerator
      Overrides:
      nextInt in class Random
    • nextBoolean

      public boolean nextBoolean()
      Specified by:
      nextBoolean in interface RandomGenerator
      Overrides:
      nextBoolean in class Random
    • nextFloat

      public float nextFloat()
      Specified by:
      nextFloat in interface RandomGenerator
      Overrides:
      nextFloat in class Random
    • nextDouble

      public double nextDouble()
      Specified by:
      nextDouble in interface RandomGenerator
      Overrides:
      nextDouble in class Random
    • nextGaussian

      public double nextGaussian()
      Specified by:
      nextGaussian in interface RandomGenerator
      Overrides:
      nextGaussian in class Random
    • nextBytes

      public void nextBytes(byte[] bytes)
      Specified by:
      nextBytes in interface RandomGenerator
      Overrides:
      nextBytes in class Random
    • serializeToString

      public String serializeToString()
      Produces a String that holds the entire state of this AlternateRandom. You can recover this state from such a String by calling deserializeFromString(String) on any AlternateRandom, which will set that AlternateRandom's state. This does not serialize any fields inherited from Random, 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

      public AlternateRandom deserializeFromString(String data)
      Given a String produced by serializeToString(), this sets the state of this AlternateRandom to the state stored in that String.This does not deserialize any fields inherited from Random, 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 by serializeToString()
      Returns:
      this AlternateRandom, after its state has been loaded from the given String
    • copy

      public AlternateRandom copy()