SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.stats.random.pseudorandom.linear
Class Lehmer

java.lang.Object
  extended by com.numericalmethod.suanshu.stats.random.pseudorandom.linear.Lehmer
All Implemented Interfaces:
LinearCongruentialGenerator, RandomLongGenerator, RandomNumberGenerator

public class Lehmer
extends java.lang.Object
implements LinearCongruentialGenerator

Lehmer proposed a general linear congruential generator that generates pseudo-random numbers in [0, 1]. It has this form:

 xi+1 = (a * xi + c) mod m
 ui+1 = xi+1 / m
 

We take c to be 0 because Marsaglia shows that there is little additional generality when c ≠ 0. However, there are restrictions placed on the selection of (a, m) and the seed. For example,

This class is essentially doing what Random.next(int) is doing (for a specific pair a and m), but it computes (ax mod m) in integer arithmetic without overflow under certain conditions. In addition, this allows customerized multiplier and modulus.

This class is the most fundamental building block for all linear random number generation algorithms in this library.

See Also:

Field Summary
 long a
          the multiplier
 long m
          the modulus
 long q
          m / a
 long r
          m % a
 
Constructor Summary
Lehmer()
          Construct a Lehmer (pure) linear congruential generator using default values.
Lehmer(long a, long m, long seed)
          Construct a Lehmer (pure) linear congruential generator.
Lehmer(long a, long m, long k, long seed)
          Construct a skipping ahead Lehmer (pure) linear congruential generator.
 
Method Summary
 long modulus()
          Get the modulus of this linear congruential generator.
 double nextDouble()
          Get the next random double between 0 and 1.
 long nextLong()
          All built-in linear random number generators in this library ultimately call this function to generate random numbers.
 int order()
          Get the order of recursion.
 void seed(long... seeds)
          Seed the random number generator to produce repeatable sequences.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

a

public final long a
the multiplier


m

public final long m
the modulus


q

public final long q
m / a


r

public final long r
m % a

Constructor Detail

Lehmer

public Lehmer(long a,
              long m,
              long seed)
Construct a Lehmer (pure) linear congruential generator.

Suggested values are:

This implementation computes the next random number in long arithmetic without overflow. It is based on L'Ecuyer, P. (1988).

Note that a cannot be too big.

Parameters:
a - multiplier
m - modulus
seed - the seed. It should not be zero.
See Also:
  • "Monte Carlo Methods in Financial Engineering. Paul Glasserman. 2004."
  • "L'Ecuyer, P. (1988) Efficient and portable combined random number generators. Communications of the ACM 31:742-749, 774. Correspondence 32:1019-1024."

Lehmer

public Lehmer(long a,
              long m,
              long k,
              long seed)
Construct a skipping ahead Lehmer (pure) linear congruential generator. The pseudo-random sequence is a subset of the original Lehmer sequence, taking every k value. Equivalently, this call is the same as
Lehmer((a^k)%m, m, seed)

We compute (a^k)%m more efficiently.

Note that a cannot be too big.

Parameters:
a - multiplier
m - modulus
k - exponent
seed - the seed. It should not be zero.

Lehmer

public Lehmer()
Construct a Lehmer (pure) linear congruential generator using default values. a = 40014; m = 2147483563; seed = 8682522807148012L + System.nanoTime()

Method Detail

seed

public void seed(long... seeds)
Description copied from interface: RandomNumberGenerator
Seed the random number generator to produce repeatable sequences.

Specified by:
seed in interface RandomNumberGenerator
Parameters:
seeds - the seeds

order

public int order()
Description copied from interface: LinearCongruentialGenerator
Get the order of recursion.

Specified by:
order in interface LinearCongruentialGenerator
Returns:
the order of recursion

modulus

public long modulus()
Description copied from interface: LinearCongruentialGenerator
Get the modulus of this linear congruential generator.

Specified by:
modulus in interface LinearCongruentialGenerator
Returns:
m

nextLong

public long nextLong()
All built-in linear random number generators in this library ultimately call this function to generate random numbers. This particular function is thread safe using non-blocking synchronization. This in turn ensures thread-safety for all these rngs.

If you are to write your own rng, you should either call this function, or have your own synchronization mechanism.

Specified by:
nextLong in interface RandomLongGenerator
Returns:
a random long number
See Also:
"Java Concurrency in Practice. Brian Goetz, Tim Peierls, Joshua Bloch and Joseph Bowbeer."

nextDouble

public double nextDouble()
Get the next random double between 0 and 1.

Specified by:
nextDouble in interface RandomNumberGenerator
Returns:
the next random number between 0 and 1

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.