# SuanShu, a Java numerical and statistical library

## com.numericalmethod.suanshu.stats.random.univariate.uniform.linear Class Lehmer

java.lang.Object
com.numericalmethod.suanshu.stats.random.univariate.uniform.linear.Lehmer

All Implemented Interfaces:
Seedable, RandomLongGenerator, RandomNumberGenerator, LinearCongruentialGenerator

public class Lehmerextends Objectimplements 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. There are restrictions placed on the selection of (a, m) and the seed. For example,
• the seed must be co-prime to m;
• the modulus m is a prime number or a power of a prime number;
• the multiplier a is an element of high multiplicative order modulo m
This implementation 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, it allows customized multiplier and modulus. This class is the most fundamental building block for all linear random number generation algorithms in this library.

See Also:
• "Lehmer, D.H. (1951) Mathematical methods in large-scale computing units, p.141-146. Proceedings of the Second Symposium on Large Scale Digital Computing Machinery. Harvard University Press, Cambridge, Mass."
• Wikipedia: Lehmer random number generator

Constructor Summary
Lehmer()
Construct a Lehmer (pure) linear congruential generator.
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.
 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/vector/scenario generator to produce repeatable experiments.

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

### Lehmer

public Lehmer(long a,
long m,
long seed)
Construct a Lehmer (pure) linear congruential generator. Suggested values are:
• m = 231 - 1 = 2147483647; a = 16807 (inferior to the other 3)
• m = 231 - 1 = 2147483647; a = 39373
• m = 2147483399; a = 40692
• m = 2147483563; a = 40014
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 - the multiplier
m - the modulus
seed - the seed. It should not be zero.
See Also:
• "Paul Glasserman, Monte Carlo Methods in Financial Engineering, 2004."
• "P. L'Ecuyer, "Efficient and portable combined random number generators," Communications of the ACM 31:742-749, 774, Correspondence 32:1019-1024, 1988."

### 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) 
This implementation computes (a^k)%m more efficiently. Note that a cannot be too big.

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

### Lehmer

public Lehmer()
Construct a Lehmer (pure) linear congruential generator.

Method Detail

### seed

public void seed(long... seeds)
Description copied from interface: Seedable
Seed the random number/vector/scenario generator to produce repeatable experiments.

Specified by:
seed in interface Seedable
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:
the modulus

### 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 thus made 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 a long number
See Also:
"Brian Goetz, Tim Peierls, Joshua Bloch and Joseph Bowbeer. Java Concurrency in Practice."

### nextDouble

public double nextDouble()
Description copied from interface: RandomNumberGenerator
Get the next random double.

Specified by:
nextDouble in interface RandomNumberGenerator
Returns:
the next random number

Copyright © 2010-2014 Numerical Method Incorporation Limited. All Rights Reserved.