com.internationalnetwork.math.random
Class XORShift

Object
  extended by com.internationalnetwork.math.random.XORShift

public class XORShift
extends Object

The XORShift class is a consistent alternative to Java's built-in java.util.Random class.  Although it doesn't offer all the same methods, the key methods are provide simple and reliable long-sequence pseudo-random number generation.  Steps have been taken in this algorithm to minimize processor overhead, and to ensure the sequence is not repeated for as long as possible (all pseudo-random number generators suffer from the same problem in that the generated sequences eventually repeat).

The algorithm itself uses a series of three eXclusive OR instructions combined with bit-wise Shift operations, all acting on the current seed which doubles as the random number.  Unlike more complicated math operations, which are typically utilized by other pseudo-random number generators, these operations are very fast because they consume fewer CPU cycles (this may be important to game developers, or other developers who are developing other time-sensitive applications).

This algorithm will never generate zeros (unless the seed is set to 0, in which case every number will always be zero).  This is the infamous short-coming of this algorithm.


Field Summary
static String VERSION
          Version number of this Package (read-only).
 
Constructor Summary
XORShift(long seed)
          Instantiate a new XORShift object with the specified seed.  The resulting pseudo-random number sequence is of "medium quality" which is an improvement over the default java.util.Random class.
 
Method Summary
 long currentLong()
          Calculate and return the current pseudo-random number.
 int nextInt()
          Return the next pseudo-random number.
 long nextLong()
          Calculate and return the next pseudo-random number.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
Version number of this Package (read-only).

See Also:
Constant Field Values
Constructor Detail

XORShift

public XORShift(long seed)
Instantiate a new XORShift object with the specified seed.  The resulting pseudo-random number sequence is of "medium quality" which is an improvement over the default java.util.Random class.

IMPORTANT:  If your seed is zero, then every result will also be zero, which renders the numeric sequence as trivially predictable.  This is the infamous short-coming of this algorithm.

Parameters:
seed - Seed that the random number generator is initialized with.
Method Detail

currentLong

public long currentLong()
Calculate and return the current pseudo-random number.


nextInt

public int nextInt()
Return the next pseudo-random number.


nextLong

public long nextLong()
Calculate and return the next pseudo-random number.