Class Lehmer
- java.lang.Object
-
- dev.nm.stat.random.rng.univariate.uniform.linear.Lehmer
-
- All Implemented Interfaces:
RandomLongGenerator
,RandomNumberGenerator
,LinearCongruentialGenerator
,Seedable
public class Lehmer extends Object implements LinearCongruentialGenerator
Lehmer proposed a general linear congruential generator that generates pseudo-random numbers in [0, 1]. It has this form:
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,xi+1 = (a * xi + c) mod m ui+1 = xi+1 / m
- 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
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
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
modulus()
Get the modulus of this linear congruential generator.double
nextDouble()
Get the next randomdouble
.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.
-
-
-
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
- Parameters:
a
- the multiplierm
- the modulusseed
- 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
This implementation computes (a^k)%m more efficiently. Note that a cannot be too big.Lehmer((a^k)%m, m, seed)
- Parameters:
a
- the multiplierm
- the modulusk
- the exponentseed
- 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.
-
order
public int order()
Description copied from interface:LinearCongruentialGenerator
Get the order of recursion.- Specified by:
order
in interfaceLinearCongruentialGenerator
- 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 interfaceLinearCongruentialGenerator
- 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 interfaceRandomLongGenerator
- 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 randomdouble
.- Specified by:
nextDouble
in interfaceRandomNumberGenerator
- Returns:
- the next random number
-
-