Java Program to Implement the linear congruential generator for Pseudo Random Number Generation

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.

//This is a sample program to generate random numbers based on linear congruential generator
import java.math.BigInteger;
import java.util.Random;
 
public class Linear_Congruential_Random_Numbers 
{
    public static void main(String args[])
    {
        BigInteger n = new BigInteger(16, new Random(){});
        Random rand = new Random();
        BigInteger m = new BigInteger("65535");//2^16
 
        for(int i=0; i<5; i++)
        {
            System.out.print(n+", ");
            BigInteger a = new BigInteger(16, new Random(){});
            BigInteger c = new BigInteger(16, new Random(){});
            n = ((a.multiply(a)).add(c)).mod(m);
        }
        System.out.println("... ");
    }
}

Output:

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

Related posts:

Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Write/Read cookies using HTTP and Read a file from the internet
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Perform Stooge Sort
Spring Data JPA @Query
Command-Line Arguments in Java
Set Interface trong Java
Tìm hiểu về Web Service
Java Program to Implement the String Search Algorithm for Short Text Sizes
Configure a RestTemplate with RestTemplateBuilder
Disable DNS caching
Static Content in Spring WebFlux
Java Program to Implement Quick sort
Java InputStream to String
What is Thread-Safety and How to Achieve it?
Java Program to Implement Dijkstra’s Algorithm using Set
Upload and Display Excel Files with Spring MVC
Spring Boot - File Handling
Spring Data MongoDB Transactions
Convert Character Array to String in Java
Count Occurrences of a Char in a String
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Jackson – Unmarshall to Collection/Array
Extract links from an HTML page
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Spring’s RequestBody and ResponseBody Annotations
A Guide to System.exit()
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Implement Hash Tables