Class 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:
    
     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

      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.
    • 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
      • 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."