Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions linear_congruential_generator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.*;
class GFG {
static void lcm(int seed, int mod, int multiplier,
int inc, int[] randomNums,
int noOfRandomNum)
{

randomNums[0] = seed;


for (int i = 1; i < noOfRandomNum; i++) {

// Follow the linear congruential method
randomNums[i]
= ((randomNums[i - 1] * multiplier) + inc)
% m;
}
}

// Driver code
public static void main(String[] args)
{

// Seed value
int seed = 5;

// Modulus parameter
int mod = 7;

// Multiplier term
int multiplier = 3;
int inc = 3;int noOfRandomNum = 10;

// To store random numbers
int[] randomNums = new int[noOfRandomNum];

lcm(seed, mod, multiplier, inc, randomNums,
noOfRandomNum);

for (int i = 0; i < noOfRandomNum; i++) {
System.out.print(randomNums[i] + " ");
}
}
}