Class XorShift1024StarPhiRandom

java.lang.Object
java.util.Random
it.unimi.dsi.util.XorShift1024StarPhiRandom
All Implemented Interfaces:
Serializable, java.util.random.RandomGenerator

public class XorShift1024StarPhiRandom extends Random
A fast, high-quality pseudorandom number generator that combines a long-period instance of George Marsaglia's Xorshift generators (described in “Xorshift RNGs”, Journal of Statistical Software, 8:1−6, 2003) with a multiplication. More information can be found at our PRNG page.

By using the supplied jump() method it is possible to generate non-overlapping long sequences for parallel computations. This class provides also a split() method to support recursive parallel computations, in the spirit of SplittableRandom.

Warning: before release 2.6.3, the split() method would not alter the state of the caller, and it would return instances initialized in the same way if called multiple times. This was a major mistake in the implementation and it has been fixed, but as a consequence the output of the caller after a call to split() is now different, and the result of split() is initialized in a different way.

Note that this is not a secure generator.

See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface java.util.random.RandomGenerator

    java.util.random.RandomGenerator.ArbitrarilyJumpableGenerator, java.util.random.RandomGenerator.JumpableGenerator, java.util.random.RandomGenerator.LeapableGenerator, java.util.random.RandomGenerator.SplittableGenerator, java.util.random.RandomGenerator.StreamableGenerator
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new generator seeded using Util.randomSeed().
    Creates a new generator using a given seed.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    The jump function for this generator.
    protected int
    next(int bits)
     
    boolean
     
    void
    nextBytes(byte[] bytes)
     
    double
     
    double
    Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence, using a fast multiplication-free method which, however, can provide only 52 significant bits.
    float
     
    int
     
    int
    nextInt(int n)
     
    long
     
    long
    nextLong(long n)
    Returns a pseudorandom uniformly distributed long value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
    void
    setSeed(long seed)
    Sets the seed of this generator.
    void
    setState(long[] state, int p)
    Sets the state of this generator.
    Returns a new instance that shares no mutable state with this instance.

    Methods inherited from class java.util.Random

    doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, nextGaussian

    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
  • Constructor Details

    • XorShift1024StarPhiRandom

      public XorShift1024StarPhiRandom()
      Creates a new generator seeded using Util.randomSeed().
    • XorShift1024StarPhiRandom

      public XorShift1024StarPhiRandom(long seed)
      Creates a new generator using a given seed.
      Parameters:
      seed - a seed for the generator.
  • Method Details

    • next

      protected int next(int bits)
      Overrides:
      next in class Random
    • nextLong

      public long nextLong()
      Specified by:
      nextLong in interface java.util.random.RandomGenerator
      Overrides:
      nextLong in class Random
    • nextInt

      public int nextInt()
      Specified by:
      nextInt in interface java.util.random.RandomGenerator
      Overrides:
      nextInt in class Random
    • nextInt

      public int nextInt(int n)
      Specified by:
      nextInt in interface java.util.random.RandomGenerator
      Overrides:
      nextInt in class Random
    • nextLong

      public long nextLong(long n)
      Returns a pseudorandom uniformly distributed long value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The algorithm used to generate the value guarantees that the result is uniform, provided that the sequence of 64-bit values produced by this generator is.
      Parameters:
      n - the positive bound on the random number to be returned.
      Returns:
      the next pseudorandom long value between 0 (inclusive) and n (exclusive).
    • nextDouble

      public double nextDouble()
      Specified by:
      nextDouble in interface java.util.random.RandomGenerator
      Overrides:
      nextDouble in class Random
    • nextDoubleFast

      public double nextDoubleFast()
      Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence, using a fast multiplication-free method which, however, can provide only 52 significant bits.

      This method is faster than nextDouble(), but it can return only dyadic rationals of the form k / 2−52, instead of the standard k / 2−53.

      The only difference between the output of this method and that of nextDouble() is an additional least significant bit set in half of the returned values. For most applications, this difference is negligible.

      Returns:
      the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence, using 52 significant bits only.
    • nextFloat

      public float nextFloat()
      Specified by:
      nextFloat in interface java.util.random.RandomGenerator
      Overrides:
      nextFloat in class Random
    • nextBoolean

      public boolean nextBoolean()
      Specified by:
      nextBoolean in interface java.util.random.RandomGenerator
      Overrides:
      nextBoolean in class Random
    • nextBytes

      public void nextBytes(byte[] bytes)
      Specified by:
      nextBytes in interface java.util.random.RandomGenerator
      Overrides:
      nextBytes in class Random
    • jump

      public void jump()
      The jump function for this generator. It is equivalent to 2512 calls to nextLong(); it can be used to generate 2512 non-overlapping subsequences for parallel computations.
    • split

      public XorShift1024StarPhiRandom split()
      Returns a new instance that shares no mutable state with this instance. The sequence generated by the new instance depends deterministically from the state of this instance, but the probability that the sequence generated by this instance and by the new instance overlap is negligible.

      Warning: before release 2.6.3, this method would not alter the state of the caller, and it would return instances initialized in the same way if called multiple times. This was a major mistake in the implementation and it has been fixed, but as a consequence the output of this instance after a call to this method is now different, and the returned instance is initialized in a different way.

      Returns:
      the new instance.
    • setSeed

      public void setSeed(long seed)
      Sets the seed of this generator.

      The argument will be used to seed a SplitMix64RandomGenerator, whose output will in turn be used to seed this generator. This approach makes “warmup” unnecessary, and makes the probability of starting from a state with a large fraction of bits set to zero astronomically small.

      Overrides:
      setSeed in class Random
      Parameters:
      seed - a seed for the generator.
    • setState

      public void setState(long[] state, int p)
      Sets the state of this generator.

      The internal state of the generator will be reset, and the state array filled with the provided array.

      Parameters:
      state - an array of 16 longs; at least one must be nonzero.
      p - the internal index.