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:
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
m, a, c, and X0 should be chosen appropriately to get a period almost equal to m.
For a = 1, it will be additive congruence method.
For c = 0, it will be the multiplicative congruence method.
- Choose the seed value X0, 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 a storage to keep the generated random numbers (here, vector is considered) of size noOfRandomNums.
- Initialize the 0th index of the vector with the seed value.
- For 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:
5 4 1 6 0 3 5 4 1 6
The literal meaning of pseudo is false. These random numbers are called pseudo because some known arithmetic procedure is utilized to generate. Even the generated sequence forms a pattern hence the generated number seems to be random but may not be truly random.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Multiplicative Congruence method for generating Pseudo Random Numbers
- Additive Congruence method for generating Pseudo Random Numbers
- Pseudo Random Number Generator (PRNG)
- Erdos Renyl Model (for generating Random Graphs)
- Zeller's Congruence | Find the Day for a Date
- Generating numbers that are divisor of their right-rotations
- Discrete Maths | Generating Functions-Introduction and Prerequisites
- Mathematics | Generating Functions - Set 2
- Find number of solutions of a linear equation of n variables
- Gaussian Elimination to Solve Linear Equations
- Linear Diophantine Equations
- Mathematics | L U Decomposition of a System of Linear Equations
- Mathematics | System of Linear Equations
- Solve the Linear Equation of Single Variable
- Finding Median of unsorted Array in linear time using C++ STL
- Program to implement Linear Extrapolation
- Second Order Linear Differential Equations
- Data Science - Solving Linear Equations
- Data Science | Solving Linear Equations
- ML | Linear Algebra Operations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.