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)
- Zeller's Congruence | Find the Day for a Date
- Modular multiplicative inverse
- Multiplicative order
- Modular multiplicative inverse from 1 to n
- Print all multiplicative primes <= N
- Count of multiplicative partitions of N
- How to write a Pseudo Code?
- Generating numbers that are divisor of their right-rotations
- Discrete Maths | Generating Functions-Introduction and Prerequisites
- Mathematics | Generating Functions - Set 2
- Generating OTP (One time Password) in PHP
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Select a random number from stream, with O(1) space
- Random number generator in arbitrary probability distribution fashion
- Mathematics | Random Variables
- 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.