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 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 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
How to Generate a Random Undirected Graph for a Given Number of Edges in Java? An undirected graph is graph, i.e, a set of objects (called vertices or nodes) that are connected together, where all the edges are bidirectional. An undirected graph is sometimes called an undirected network. In contrast, a graph where the edges point in a direction is called a directed graph. Undi
3 min read
Generate Random Numbers Using Middle Square Method in Java This method was proposed by Van Neumann. In this method, we have a seed and then the seed is squared and its midterm is fetched as the random number. Consider we have a seed having N digits we square that number to get a 2N digits number if it doesn't become 2N digits we add zeros before the number
3 min read
How to Generate a Random Directed Acyclic Graph for a Given Number of Edges in Java? A Directed Acyclic Graph is a directed graph with no directed cycles. In a directed graph, the edges are connected so that each edge only goes one way. A directed acyclic graph means that the graph is not cyclic, or that it is impossible to start at one point in the graph and traverse the entire gra
5 min read
How to Add Random Number to an Array in Java? To generate an array of integers with random values the nextInt() method from the java.util.Random class is used. From the random number generator sequence, this method returns the next random integer value.Assigning a Random Value to an ArrayWe can assign random values to an array by two approaches
2 min read
Java Program For Cloning A Linked List With Next And Random Pointer In O(1) Space Given a linked list having two pointers in each node. The first one points to the next node of the list, however, the other pointer is random and can point to any node of the list. Write a program that clones the given list in O(1) space, i.e., without any extra space. Examples: Input : Head of the
4 min read