Java Program to Implement Park-Miller Random Number Generation Algorithm

«
»
This is java program to generate the random numbers, using the Park-Miller algorithm.Park–Miller random number generator (after Stephen K. Park and Keith W. Miller), is a variant of linear congruential generator (LCG) that operates in multiplicative group of integers modulo n. A general formula of a random number generator (RNG) of this type is, x(n+1) = g*x(n) mod n.

Here is the source code of the Java Program to Implement Park-Miller Random Number Generation Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a sample program to random numbers using Park Miller Random Numbers algorithm
  2. public class Park_Miller_Random_Numbers 
  3. {
  4.     static final long m = 2147483647L;
  5.     static final long a = 48271L;
  6.     static final long q = 44488L;
  7.     static final long r = 3399L;
  8.  
  9.     static long r_seed = 12345678L; 
  10.  
  11.     public static double uniform ()
  12.     {
  13.         long hi = r_seed / q;
  14.         long lo = r_seed - q * hi;
  15.         long t = a * lo - r * hi;
  16.         if (t > 0)
  17.             r_seed = t;
  18.         else
  19.             r_seed = t + m;
  20.         return r_seed;
  21.     }
  22.  
  23.     public static void main (String[] argv)
  24.     {
  25.         double[] A = new double [10];
  26.  
  27.         for (int i=0; i<5; i++) 
  28.             A[i] = uniform();
  29.  
  30.         for (int i=0; i<5; i++) 
  31.             System.out.print ("  " + A[i]);    
  32.     }
  33. }

Output:

advertisement
$ javac Park_Miller_Random_Numbers.java
$ java Park_Miller_Random_Numbers
1.085252519E9  5.08259731E8  1.352291773E9  1.563240271E9  8.90733155E8 ...

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.