Java Program to Implement Park-Miller Random Number Generation Algorithm
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
Similar Reads
Java Program to Implement Inversion Method for Random Number Generation Here we will be going through the inversion method for random number generation in java. So basically we will be illustrating two approaches which are as follows: Shuffling elements in an arrayUsing Collection.shuffle() method Important Method here namely is setSeed(). It is used to set the seed via
9 min read
Java Program to Implement the Linear Congruential Generator for Pseudo Random Number Generation Linear Congruential Method is a class of Pseudo-Random Number Generator (PRNG) algorithms used for generating sequences of random-like numbers in a specific range. This method can be defined as: Xi+1 = aXi + c mod m where, X, is the sequence of pseudo-random numbers m, ( > 0) the modulus a, (0, m
3 min read
Java Program to Implement the RSA Algorithm RSA or RivestâShamirâAdleman is an algorithm employed by modern computers to encrypt and decrypt messages. It is an asymmetric cryptographic algorithm. Asymmetric means that there are two different keys. This is also called public-key cryptography because one among the keys are often given to anyone
3 min read
Java Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31
2 min read
Java Program to Implement Naor-Reingold Pseudo Random Function Naor-Reingold Pseudo-Random Function is a function of generating random numbers. Moni Naor and Omer Reingold described efficient constructions for various cryptographic primitives in the private key as well as public-key cryptography. Example: Input : N = 5 Output: 9.0, 9.0, 3.0, 9.0, 3.0 Input : N
2 min read
Java Program to Generate Random Numbers Using Multiply with Carry Method In computer science, multiply-with-carry (MWC) is a method invented by George Marsaglia for generating sequences of random integers based on an initial set from two to many thousands of randomly chosen seed values. The main advantage of the MWC method is that it invokes simple computer integer arith
2 min read
Java Program to Guess a Random Number in a Range Write a program that generates a random number and asks the user to guess what the number is. If the userâs guess is higher than the random number, the program should display Too high, try again. If the userâs guess is lower than the random number, the program should display Too low, try again. The
2 min read
Java Program to Generate N Number of Passwords of Length M Each Java program to generate N number of passwords each of length M. The number of passwords returned doesnât exceed the length M. Example: Input : N = 1, M = 3 Output: 571 Input : N = 2, M = 4 Output: 5671 1987 Approach: Import random package for creating random numbers.Initialize variables N and M.Cre
2 min read
Java Program to Implement Shunting Yard Algorithm The shunting yard algorithm is used to convert the infix notation to reverse Polish notation. The postfix notation is also known as the Reverse Polish Notation (RPN). The algorithm was named a âShunting yardâ because its activity is similar to a railroad shunting yard. It is a method for representin
6 min read
Java Program to Generate Random Hexadecimal Bytes To generate Random Hexadecimal Bytes, first, a random byte can be generated in decimal form using Java.util.Random.nextInt() and then it can be converted to hexadecimal form using Integer.toHexString() method. 1. Java.util.Random.nextInt() The nextInt() method is used to obtain the next integer from
2 min read