Skip to content
Related Articles

Related Articles

Java Program to Implement Park-Miller Random Number Generation Algorithm

View Discussion
Improve Article
Save Article
  • Last Updated : 15 Dec, 2020

Park–Miller random number generator is also known as Lehmer random number generator. A general formula of a random number generator (RNG) of this type is, Xk+1 = a * xk mod m

Where 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, and the seed X0 is coprime to m.  

Algorithm of Park-Miller

1) Declare variables m, a, q, r and r_seed(X0) as constant variables and assign 

m = 2145678965L;
a = 48271L;
q = 44488L;
r = 3399L;
r_seed = 12345678L

Here, the value of constants m and r_seed are chosen in such a way that the GCD(m, r_seed) = 1. The modulus can also be chosen as a prime number, making the choice of a coprime seed trivial (any 0 < X0 < m will do). Here, the values a, q, r are constants.

2) Declare function uniform with double as return-type.

     2.1) Declare variable hi, lo, t.

     2.2) set variables hi, lo, t as following : 

              2.2.1) hi= as seed divided by b

              2.2.2) lo = seed – b * hi

              2.2.3) t = a * lo – c * hi

Where hi and li are the highest and lowest numbers in a range respectively for a random number for particular m and r_seed(X0).

     2.3) check if (t > 0) i.e; generated random number is positive then set  r_seed = t.

     2.4) else set r_seed = t + m. // generated random number is negative

     2.5) return seed from function uniform.

3) In the main method

     3.1) Start a loop from i=0 to 10

             3.1.1)Call the function random and store the results for each iteration.

     3.2)Print the results

5)End

Example: The below program Implements Park-Miller Random Number Generation Algorithm.

Note: Each iteration of this code will produce different output.

Java




// Java implementation of Park-Miller algorithm
  
public class Park_Miller_Random_Numbers {
  
    // m is coprime to seed r_seed
    static final long m = 2147483647L;
  
    // constants
    static final long a = 48271L;
    static final long q = 44488L;
    static final long r = 3399L;
  
    // take a r_seed that is coprime to m
    static long r_seed = 12345678L;
  
    public static double uniform()
    {
        // highest and lowest for
        // a random number generation
        // range
        long hi = r_seed / q;
        long lo = r_seed - q * hi;
  
        // calculate random number
        long t = a * lo - r * hi;
  
        // if positive
        if (t > 0)
            r_seed = t;
        else
            r_seed = t + m;
        return r_seed;
    }
  
    public static void main(String[] argv)
    {
        double[] A = new double[10];
  
        for (int i = 0; i < 5; i++)
            A[i] = uniform();
  
        for (int i = 0; i < 5; i++)
            System.out.print("  " + A[i]);
    }
}


Output

  1.085252519E9  5.08259731E8  1.352291773E9  1.563240271E9  8.90733155E8

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!