# Java Program to Implement Linear Congruential Method for Generating Pseudo Random Number

«
»
This is java program to generate a random numbers, using linear congruential generator. The formula for next random number in the sequence is x(n+1) = {a*x(n)+c}mod m, where x(n+1) is current number to generate, x(n) is previously generated, a is multiplier, c is additive term and m is modulus.

Here is the source code of the Java Program to Implement the linear congruential generator for Pseudo Random Number Generation. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `//This is a sample program to generate random numbers based on linear congruential generator`
2. `import java.math.BigInteger;`
3. `import java.util.Random;`
4. ` `
5. `public class Linear_Congruential_Random_Numbers `
6. `{`
7. `    public static void main(String args[])`
8. `    {`
9. `        BigInteger n = new BigInteger(16, new Random(){});`
10. `        Random rand = new Random();`
11. `        BigInteger m = new BigInteger("65535");//2^16`
12. ` `
13. `        for(int i=0; i<5; i++)`
14. `        {`
15. `            System.out.print(n+", ");`
16. `            BigInteger a = new BigInteger(16, new Random(){});`
17. `            BigInteger c = new BigInteger(16, new Random(){});`
18. `            n = ((a.multiply(a)).add(c)).mod(m);`
19. `        }`
20. `        System.out.println("... ");`
21. `    }`
22. `}`

Output:

```\$ javac Linear_Congruential_Random_Numbers.java
\$ java Linear_Congruential_Random_Numbers
5107, 48257, 43654, 50875, 12815, ...```

Sanfoundry Global Education & Learning Series – 1000 Java Programs. 