Java Program to Implement the Linear Congruential Generator for Pseudo Random Number Generation Last Updated : 17 Jul, 2021 Comments Improve Suggest changes Like Article Like Report 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) the multiplier c, (0, m) the increment X0, [0, m) – Initial value of sequence known as seed Note: m, a, c, and X0 should be chosen appropriately to get a period almost equal to m. For a = 1, it will be the additive congruence method.For c = 0, it will be the multiplicative congruence method. Approach: The seed value X0 is chosen, Modulus parameter m, Multiplier term a, and increment term c.Initialize the required amount of random numbers to generate (say, an integer variable noOfRandomNums).Define storage to keep the generated random numbers (here, the vector is considered) of size noOfRandomNums.Initialize the 0th index of the vector with the seed value.For the rest of the indexes follow the Linear Congruential Method to generate the random numbers. randomNums[i] = ((randomNums[i – 1] * a) + c) % m Finally, return the random numbers. Below is the implementation of the above approach: Java // Java implementation of the above approach import java.util.*; class GFG { // Function to generate random numbers static void lcm(int seed, int mod, int multiplier, int inc, int[] randomNums, int noOfRandomNum) { // Initialize the seed state randomNums[0] = seed; // Traverse to generate required // numbers of random numbers for (int i = 1; i < noOfRandomNum; i++) { // Follow the linear congruential method randomNums[i] = ((randomNums[i - 1] * multiplier) + inc) % m; } } // Driver code public static void main(String[] args) { // Seed value int seed = 5; // Modulus parameter int mod = 7; // Multiplier term int multiplier = 3; // Increment term int inc = 3; // Number of Random numbers // to be generated int noOfRandomNum = 10; // To store random numbers int[] randomNums = new int[noOfRandomNum]; // Function Call lcm(seed, mod, multiplier, inc, randomNums, noOfRandomNum); // Print the generated random numbers for (int i = 0; i < noOfRandomNum; i++) { System.out.print(randomNums[i] + " "); } } } Output5 4 1 6 0 3 5 4 1 6 The literal meaning of pseudo is false or imaginary. These random numbers are called pseudo because some known arithmetic procedure is utilized to generate them. Even the generated sequence forms a pattern hence the generated number seems to be random but may not be truly random. Comment More infoAdvertise with us Next Article Java Program to Implement the Linear Congruential Generator for Pseudo Random Number Generation G gunjanpaul Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Implement Park-Miller Random Number Generation Algorithm 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 ord 3 min read 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 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 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 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 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 How to Generate Unique Positive Long Number in Java? Java provides different ways to generate random numbers, using some built-in methods and classes, but most of them do generate unique positive long numbers like java.util.Random class, Math.random method and ThreadLocalRandom class, and many others. But in most of these either with randomness unique 3 min read Java Program to Find GCD and LCM of Two Numbers Using Euclidâs Algorithm GCD or the Greatest Common Divisor of two given numbers A and B is the highest number dividing both A and B completely, i.e., leaving remainder 0 in each case. LCM or the Least Common Multiple of two given numbers A and B is the Least number which can be divided by both A and B, leaving remainder 0 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 Like