it.unimi.dsi.util
Class XorShiftStarRandom

java.lang.Object
  extended by java.util.Random
      extended by it.unimi.dsi.util.XorShiftStarRandom
All Implemented Interfaces:
Serializable

public class XorShiftStarRandom
extends Random

A fast, high-quality 64-bit pseudorandom number generator that combines George Marsaglia's Xorshift generators (described in “Xorshift RNGs”, Journal of Statistical Software, 8:1−6, 2003) with a multiplication. Note that this is not a cryptographic-strength pseudorandom number generator, but its quality is preposterously higher than Random's (and its cycle length is 264 − 1).

On an Intel i5, calls to nextLong(long) range from 20% faster to fifty times faster than ThreadLocalRandom's, and calls to nextInt(int) range from 20% slower to two times faster, depending on the argument. Timings are orders of magnitude faster than Random's, but Random is slowed down by the fact of being thread safe, so the comparison is not fair.

This class extends Random, overriding (as usual) the Random.next(int) method. Nonetheless, since the generator is inherently 64-bit also Random.nextInt(), Random.nextInt(int), Random.nextLong() and Random.nextDouble() have been overridden for speed (preserving, of course, Random's semantics).

Parameter choice

There are five parameters to choose in a pseudorandom number generator of this kind: the three shift values, the type of shift, and the multiplier. Numerical Recipes (third edition, Cambridge University Press, 2007) suggests a choice of parameters from which we take only the multiplier (actually, proposed by Pierre L'Ecuyer in “Tables of linear congruential generators of different sizes and good lattice structure”, Math. Comput., 68(225):249−260, 1999). The remaining parameters have been set following extensive experimentation on the 2200 possible choices using TestU01 and Dieharder. More details will appear in a forthcoming paper.

Warning: this class is still experimental, and different parameters might be used in the future.

Notes

The lower bits of this generator are of higher quality than the upper bits. Thus, masking the lower bits is a safe and effective way to obtain small random numbers. The code in this class methodically extracts lower bits, rather than upper bits, whenever a subset of bits is necessary.

See Also:
Serialized Form

Constructor Summary
XorShiftStarRandom()
           
XorShiftStarRandom(long seed)
          Creates a new generator using a given seed.
 
Method Summary
protected  int next(int bits)
           
 boolean nextBoolean()
           
 void nextBytes(byte[] bytes)
           
 double nextDouble()
           
 float nextFloat()
           
 int nextInt()
           
 int nextInt(int n)
           
 long nextLong()
           
 long nextLong(long n)
           
 void setSeed(long seed)
           
 
Methods inherited from class java.util.Random
nextGaussian
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XorShiftStarRandom

public XorShiftStarRandom()

XorShiftStarRandom

public XorShiftStarRandom(long seed)
Creates a new generator using a given seed.

Parameters:
seed - a nonzero seed for the generator (if zero, the generator will be seeded with -1).
Method Detail

next

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

nextLong

public long nextLong()
Overrides:
nextLong in class Random

nextInt

public int nextInt()
Overrides:
nextInt in class Random

nextInt

public int nextInt(int n)
Overrides:
nextInt in class Random

nextLong

public long nextLong(long n)

nextDouble

public double nextDouble()
Overrides:
nextDouble in class Random

nextFloat

public float nextFloat()
Overrides:
nextFloat in class Random

nextBoolean

public boolean nextBoolean()
Overrides:
nextBoolean in class Random

nextBytes

public void nextBytes(byte[] bytes)
Overrides:
nextBytes in class Random

setSeed

public void setSeed(long seed)
Overrides:
setSeed in class Random