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
Example: The below program Implements Park-Miller Random Number Generation Algorithm.
Note: Each iteration of this code will produce different output.
1.085252519E9 5.08259731E8 1.352291773E9 1.563240271E9 8.90733155E8
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.