Related Articles
Additive Congruence method for generating Pseudo Random Numbers
• Last Updated : 28 May, 2021

Additive Congruential Method is a type of linear congruential generator for generating pseudorandom numbers in a specific range. This method can be defined as:

where,

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.

Approach:

• 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 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 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:

## C++

 // C++ implementation of the// above approach #include using namespace std; // Function to generate random numbersvoid additiveCongruentialMethod(    int Xo, int m, int c,    vector<int>& randomNums,    int noOfRandomNums){     // Initialize the seed state    randomNums[0] = Xo;     // Traverse to generate required    // numbers of random numbers    for (int i = 1; i < noOfRandomNums; i++) {         // Follow the additive        // congruential method        randomNums[i]            = (randomNums[i - 1] + c)              % m;    }} // Driver Codeint main(){    int Xo = 3; // seed value    int m = 15; // modulus parameter    int c = 2; // increment term     // Number of Random numbers    // to be generated    int noOfRandomNums = 20;     // To store random numbers    vector<int> randomNums(noOfRandomNums);     // Function Call    additiveCongruentialMethod(Xo, m, c,                               randomNums, noOfRandomNums);     // Print the generated random numbers    for (int i = 0; i < noOfRandomNums; i++) {        cout << randomNums[i] << " ";    }     return 0;}

## Java

 // Java implementation of the// above approachclass GFG{ // Function to generate random numbersstatic void additiveCongruentialMethod(    int Xo, int m, int c,    int []randomNums,    int noOfRandomNums){     // Initialize the seed state    randomNums[0] = Xo;     // Traverse to generate required    // numbers of random numbers    for(int i = 1; i < noOfRandomNums; i++)    {         // Follow the additive        // congruential method        randomNums[i] = (randomNums[i - 1] + c) % m;    }} // Driver Codepublic static void main(String[] args){         // Seed value    int Xo = 3;         // Modulus parameter    int m = 15;         // Increment term    int c = 2;     // Number of Random numbers    // to be generated    int noOfRandomNums = 20;     // To store random numbers    int []randomNums = new int[noOfRandomNums];     // Function Call    additiveCongruentialMethod(Xo, m, c,                               randomNums,                               noOfRandomNums);     // Print the generated random numbers    for(int i = 0; i < noOfRandomNums; i++)    {        System.out.print(randomNums[i] + " ");    }}} // This code is contributed by PrinciRaj1992

## Python3

 # Python3 implementation of the# above approach # Function to generate random numbersdef additiveCongruentialMethod(Xo, m, c,                               randomNums,                               noOfRandomNums):     # Initialize the seed state    randomNums[0] = Xo     # Traverse to generate required    # numbers of random numbers    for i in range(1, noOfRandomNums):                 # Follow the linear congruential method        randomNums[i] = (randomNums[i - 1] + c) % m # Driver Codeif __name__ == '__main__':         # Seed value    Xo = 3         # Modulus parameter    m = 15         # Multiplier term    c = 2     # Number of Random numbers    # to be generated    noOfRandomNums = 20     # To store random numbers    randomNums=[0] * (noOfRandomNums)     # Function Call    additiveCongruentialMethod(Xo, m, c,                               randomNums,                               noOfRandomNums)     # Print the generated random numbers    for i in randomNums:        print(i, end = " ") # This code is contributed by mohit kumar 29

## C#

 // C# implementation of the// above approachusing System; class GFG{ // Function to generate random numbersstatic void additiveCongruentialMethod(    int Xo, int m, int c,    int []randomNums,    int noOfRandomNums){     // Initialize the seed state    randomNums[0] = Xo;     // Traverse to generate required    // numbers of random numbers    for(int i = 1; i < noOfRandomNums; i++)    {         // Follow the additive        // congruential method        randomNums[i] = (randomNums[i - 1] + c) % m;    }} // Driver Codepublic static void Main(String[] args){         // Seed value    int Xo = 3;         // Modulus parameter    int m = 15;         // Increment term    int c = 2;     // Number of Random numbers    // to be generated    int noOfRandomNums = 20;     // To store random numbers    int []randomNums = new int[noOfRandomNums];     // Function call    additiveCongruentialMethod(Xo, m, c,                               randomNums,                               noOfRandomNums);     // Print the generated random numbers    for(int i = 0; i < noOfRandomNums; i++)    {        Console.Write(randomNums[i] + " ");    }}} // This code is contributed by PrinciRaj1992

## Javascript

 
Output:
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 mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up