Additive Congruential 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
c [0, m), the increment
X0 [0, m), initial value of the sequence – termed as seed
m, c, X0 should be chosen appropriately to get a period almost equal to m.
- Choose the seed value X0, modulus parameter m, and increment term c.
- 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 indexes follow the Additive Congruential Method to generate the random numbers.
randomNums[i] = (randomNums[i – 1] + c) % m
Finally, return the generated random numbers.
Below is the implementation of the above approach:
3 5 7 9 11 13 0 2 4 6 8 10 12 14 1 3 5 7 9 11
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
- Multiplicative Congruence method for generating Pseudo Random Numbers
- Pseudo Random Number Generator (PRNG)
- Additive Prime Number
- Erdos Renyl Model (for generating Random Graphs)
- Zeller's Congruence | Find the Day for a Date
- 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
- 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
- Binomial Random Variables
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Random Tree Generator Using Prüfer Sequence with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.