- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Implement the Linear Congruential Generator for Pseudo Random Number Generation
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.
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.
Note - 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.
Here, we have taken some sample examples.
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 total 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.
Approach 1
In this approach, we will use the above-explained formula to generate the sequence of random numbers from the previously generated random number.
Algorithm
Step 1 - Initialize all variables with constant values. Also, initialize the 'allRandoms' array to store the random numbers.
Step 2 - Execute the generateRandom() function.
Step 3 - In the generateRandom() function, initialize the first element of the array with an initial number.
Step 4 - After that, use the loop to make total iterations equal to the 'totalNums' of times.
Step 5 - In the current iteration of the loop, store the value of '((allRandoms[i - 1] * mult) + increment) % modulas' in the allRandoms[i] position.
Step 6 - Print the random numbers.
Example
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.
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 limit value to the generated number to the bound minimum limit.