Implement Linear Congruential Generator for Pseudo-Random Number Generation in Java



In this article, we will implement the linear congruential generator for pseudo random number generation in Java. Pseudo random number generator (PRNG) are mainly used in simulations, cryptography, or mathematical tasks.

What is an LCG?

A Linear Congruential Generator (LCG) is a technique to generate a sequence of numbers that looks like random numbers but are actually determined. It is one of the reasons to call it a pseudo-random number.

The Linear Congruential Generator (LCG) technique generates a random number based on the previous number and uses linear recurrence to generate the sequence of the random number.

Mathematical Formula

We can use the below formula of LCG to generate random numbers based on the previous numbers ?

$$\mathrm{x_{n+1}=(mult\:x_{n}+\:increment\:mod\:modulus)}$$

In the above formula, the 'mod' presents the modulus operation.

  • $\mathrm{x_{n+1}}$ - Next pseudo-random number to generate.

  • $\mathrm{x_{n}}$ - Previously generated random number.

  • Mult - It is a constant for multiplication.

  • Increment - It is a constant to add.

  • Modulus - It is also a constant to perform modulus operation.

Problem Statement

We should take the value of the constant in such a way so that we can generate the proper random numbers using the LCG algorithm. Furthermore, we need to take the 'initial' number, which we can say seed value, to start generating the sequence of random numbers.

Sample examples

Input

initial = 32, modulas = 11, mult = 5, increment = 13, totalNums = 15

Output

32 8 9 3 6 10 8 9 3 6 10 8 9 3 6

Explanation - It has generated a total of 15 numbers based on the given formula.

Input

initial = 100, modulas = 74, mult = 8, increment = 37, totalNums = 5

Output

   100 23 73 29 47

Explanation - Here, numbers are generated in the range of 1 to 74. Also, it looks like random numbers.

Implementing LCG in Java

In this approach, we will use the above-explained formula to generate the sequence of random numbers from the previously generated random number.

The following are the steps of LSG for pseudo-random number generation in Java ?

  • Start by giving each variable a fixed, constant value, and create an empty array named allRandoms for storing the random numbers.
  • Call the generateRandom() function to start the process.
  • Inside generateRandom(), place an initial number in the first position of the allRandoms array.
  • Use a for loop that runs a number of times equal to the variable totalNums.
  • After the loop finishes, print out all of the random numbers from the allRandoms array.

Example

Below is an example of LSG for pseudo-random number generation in Java ?

import java.util.*;
public class Main {
   static void generateRandom(int initial, int modulas, int mult, int 
increment, int[] allRandoms, int totalNums) {
      // Initial state of Linear Congruential Generator
      allRandoms[0] = initial;
      // Genereate sequence of random numbers
      for (int i = 1; i < totalNums; i++) {
         // linear congruential method
         allRandoms[i] = ((allRandoms[i - 1] * mult) + increment) % modulas;
      }
   }
   public static void main(String[] args) {
      // Initial value, and using that, we need to start generating a random number
      int initial = 32;
      // Integer for modulo operation
      int modulas = 11;
      // Integer for multiplier
      int mult = 5;
      // Integer to add
      int increment = 13;
      // count of random numbers to be generated
      int totalNums = 15;
      // array to store random numbers
      int[] allRandoms = new int[totalNums];
      generateRandom(initial, modulas, mult, increment, allRandoms, totalNums);
      // print numbers
      System.out.println("Random numbers generated are: ");
      for (int p = 0; p < totalNums; p++) {
         System.out.print(allRandoms[p] + " ");
      }
   }
}

Output

Random numbers generated are: 
32 8 9 3 6 10 8 9 3 6 10 8 9 3 6

Time complexity: O(N) as we find N random numbers.
Space complexity: O(N) as we store all random numbers. However, we can optimize it to O(1) if we don't need to store any random numbers.

Conclusion

We learned the Linear Congruential Generator (LCG) algorithm to generate pseudo-random numbers, one of the old approaches to generating a sequence of random numbers. Programmers need to take care of the constant values to generate quality random numbers. Also, the modulus value helps us to bind the maximum limit of random numbers, and we can add a limit value to the generated number to the bound minimum limit.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-03-29T02:56:11+05:30

495 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements