Multiplicative Congruential Method (Lehmer Method) is a type of linear congruential generator for generating pseudorandom numbers in a specific range. This method can be defined as:
X, the sequence of pseudo-random numbers
m ( > 0), the modulus
a (0, m), the multiplier
X0 [0, m), initial value of the sequence – termed as seed
m, a, and X0 should be chosen appropriately to get a period almost equal to m.
- Choose the seed value ( X0 ), modulus parameter ( m ), and multiplier term ( a ).
- Initialize the required amount of random numbers to generate (say, an integer variable noOfRandomNums).
- Define a storage to keep the genrated 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 Multiplicative Congruential Method to generate the random numbers.
randomNums[i] = (randomNums[i – 1] * a) % m
Finally, return the random numbers.
Below is the implementation of the above approach:
3 6 12 9 3 6 12 9 3 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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Linear 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)
- Generating numbers that are divisor of their right-rotations
- Zeller's Congruence | Find the Day for a Date
- Multiplicative order
- Modular multiplicative inverse
- Modular multiplicative inverse from 1 to n
- Print all multiplicative primes <= N
- Count of multiplicative partitions of N
- How to write a Pseudo Code?
- Mathematics | Generating Functions - Set 2
- Generating OTP (One time Password) in PHP
- Discrete Maths | Generating Functions-Introduction and Prerequisites
- Mathematics | Random Variables
- Binomial Random Variables
- Fermat's Factorization method for large numbers
- Multiply Large Numbers using Grid Method
- Random list of M non-negative integers whose sum is N
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.