org.encog.mathutil
Class LinearCongruentialGenerator

java.lang.Object
  extended by org.encog.mathutil.LinearCongruentialGenerator

public class LinearCongruentialGenerator
extends Object

A predictable random number generator. This is useful for unit tests and benchmarks where we want random numbers, but we want them to be the same each time. This class exists on both Java and C# so it can even provide consistent random numbers over the two platforms. Random numbers are created using a LCG. http://en.wikipedia.org/wiki/Linear_congruential_generator


Field Summary
static long MAX_RAND
          The maximum rand number that the standard GCC based LCG will generate.
 
Constructor Summary
LinearCongruentialGenerator(long seed)
          Construct the default LCG.
LinearCongruentialGenerator(long modulus, long multiplier, long increment, long seed)
          Create a LCG with the specified modulus, multiplier and increment.
 
Method Summary
 long getIncrement()
           
 long getModulus()
           
 long getMultiplier()
           
 long getSeed()
           
 double nextDouble()
           
 long nextLong()
           
 double range(double min, double max)
          Generate a random number in the specified range.
 void setSeed(long seed)
          Set the seed value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_RAND

public static final long MAX_RAND
The maximum rand number that the standard GCC based LCG will generate.

See Also:
Constant Field Values
Constructor Detail

LinearCongruentialGenerator

public LinearCongruentialGenerator(long seed)
Construct the default LCG. You need only specify a seed.

Parameters:
seed - The seed to use.

LinearCongruentialGenerator

public LinearCongruentialGenerator(long modulus,
                                   long multiplier,
                                   long increment,
                                   long seed)
Create a LCG with the specified modulus, multiplier and increment. Unless you REALLY KNOW WHAT YOU ARE DOING, just use the constructor that just takes a seed. It will set these values to the same as set by the GCC C compiler. Setting these values wrong can create fairly useless random numbers.

Parameters:
modulus - The modulus for the LCG algorithm.
multiplier - The multiplier for the LCG algorithm.
increment - The increment for the LCG algorithm.
seed - The seed for the LCG algorithm. Using the same seed will give the same random number sequence each time, whether in Java or DotNet.
Method Detail

getIncrement

public long getIncrement()
Returns:
The LCG increment.

getModulus

public long getModulus()
Returns:
The LCG modulus.

getMultiplier

public long getMultiplier()
Returns:
The LCG multiplier.

getSeed

public long getSeed()
Returns:
The current seed. Set to a constant to start, thereafter the previously generated random number.

nextDouble

public double nextDouble()
Returns:
The next random number as a double between 0 and 1.

nextLong

public long nextLong()
Returns:
The next random number as a long between 0 and MAX_RAND.

range

public double range(double min,
                    double max)
Generate a random number in the specified range.

Parameters:
min - The minimum random number.
max - The maximum random number.
Returns:
The generated random number.

setSeed

public void setSeed(long seed)
Set the seed value. Setting a seed to a specific value will always result in the same sequence of numbers, whether on Java or DotNet.

Parameters:
seed - The seed value.


Copyright © 2011. All Rights Reserved.