Multiplicative Congruence method for generating Pseudo Random Numbers - GeeksforGeeks

08 Feb, 2022
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: 
    
 
where, X, the sequence of pseudo-random numbersm ( > 0), the modulusa (0, m), the multiplierX0 [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.
 
Approach: 
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 storage to keep the generated random numbers (here, the vector is considered) of size noOfRandomNums.
Initialize the 0th index of the vector with the seed value.
For the 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:
C++
Java
Python3
C#
Javascript
// C++ implementation of the// above approach#include <bits/stdc++.h>using namespace std; // Function to generate random numbersvoid multiplicativeCongruentialMethod(    int Xo, int m, int a,    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 multiplicative        // congruential method        randomNums[i]            = (randomNums[i - 1] * a) % m;    }} // Driver Codeint main(){    int Xo = 3; // seed value    int m = 15; // modulus parameter    int a = 7; // multiplier term     // Number of Random numbers    // to be generated    int noOfRandomNums = 10;     // To store random numbers    vector<int> randomNums(noOfRandomNums);     // Function Call    multiplicativeCongruentialMethod(        Xo, m, a, randomNums,        noOfRandomNums);     // Print the generated random numbers    for (int i = 0; i < noOfRandomNums; i++) {        cout << randomNums[i] << " ";    }    return 0;}
// Java implementation of the above approachimport java.util.*; class GFG{ // Function to generate random numbersstatic void multiplicativeCongruentialMethod(    int Xo, int m, int a,    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 multiplicative        // congruential method        randomNums[i] = (randomNums[i - 1] * a) % m;    }} // Driver codepublic static void main(String[] args){         // Seed value    int Xo = 3;         // Modulus parameter    int m = 15;         // Multiplier term    int a = 7;         // Number of Random numbers    // to be generated    int noOfRandomNums = 10;         // To store random numbers    int[] randomNums = new int[noOfRandomNums];         // Function Call    multiplicativeCongruentialMethod(Xo, m, a,                                     randomNums,                                     noOfRandomNums);         // Print the generated random numbers    for(int i = 0; i < noOfRandomNums; i++)    {        System.out.print(randomNums[i] + " ");    }}} // This code is contributed by offbeat
# Python3 implementation of the# above approach # Function to generate random numbersdef multiplicativeCongruentialMethod(Xo, m, a,                                     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] * a) % m # Driver Codeif __name__ == '__main__':         # Seed value    Xo = 3         # Modulus parameter    m = 15         # Multiplier term    a = 7     # Number of Random numbers    # to be generated    noOfRandomNums = 10     # To store random numbers    randomNums = [0] * (noOfRandomNums)     # Function Call    multiplicativeCongruentialMethod(Xo, m, a,                                     randomNums,                                     noOfRandomNums)     # Print the generated random numbers    for i in randomNums:        print(i, end = " ") # This code is contributed by mohit kumar 29
// C# implementation of the above approachusing System; class GFG{ // Function to generate random numbersstatic void multiplicativeCongruentialMethod(    int Xo, int m, int a,    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 multiplicative        // congruential method        randomNums[i] = (randomNums[i - 1] * a) % m;    }} // Driver codepublic static void Main(String[] args){         // Seed value    int Xo = 3;         // Modulus parameter    int m = 15;         // Multiplier term    int a = 7;         // Number of Random numbers    // to be generated    int noOfRandomNums = 10;         // To store random numbers    int[] randomNums = new int[noOfRandomNums];         // Function call    multiplicativeCongruentialMethod(Xo, m, a,                                     randomNums,                                     noOfRandomNums);         // Print the generated random numbers    for(int i = 0; i < noOfRandomNums; i++)    {        Console.Write(randomNums[i] + " ");    }}} // This code is contributed by sapnasingh4991
<script> // Javascript program to implement// the above approach // Function to generate random numbersfunction multiplicativeCongruentialMethod(    Xo, m, a,    randomNums, noOfRandomNums){           // Initialize the seed state    randomNums[0] = Xo;           // Traverse to generate required    // numbers of random numbers    for(let i = 1; i < noOfRandomNums; i++)    {                   // Follow the multiplicative        // congruential method        randomNums[i] = (randomNums[i - 1] * a) % m;    }}     // Driver Code               // Seed value    let Xo = 3;           // Modulus parameter    let m = 15;           // Multiplier term    let a = 7;           // Number of Random numbers    // to be generated    let noOfRandomNums = 10;           // To store random numbers    let randomNums = new Array(noOfRandomNums).fill(0);           // Function Call    multiplicativeCongruentialMethod(Xo, m, a,                                     randomNums,                                     noOfRandomNums);           // Print the generated random numbers    for(let i = 0; i < noOfRandomNums; i++)    {        document.write(randomNums[i] + " ");    } </script>
3 6 12 9 3 6 12 9 3 6
 
Time Complexity: O(N), where N is the total number of random numbers we need to generate.Auxiliary Space: O(1)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. 
mohit kumar 29
offbeat
sapnasingh4991
avijitmondal1998
pankajsharmagfg
surinderdawra388
Algorithms
Engineering Mathematics
Mathematical
Mathematical
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Understanding Time Complexity with Simple Examples
Introduction to Algorithms
How to write a Pseudo Code?
Relationship between number of nodes and height of binary tree
Inequalities in LaTeX
Mathematics | Walks, Trails, Paths, Cycles and Circuits in Graph
Arrow Symbols in LaTeX
Newton's Divided Difference Interpolation Formula