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:

A Guide to BitSet in Java
Fixing 401s with CORS Preflights and Spring Security
Java Program to Perform the Sorting Using Counting Sort
Service Registration with Eureka
Hướng dẫn sử dụng Lớp FilePermission trong java
Difference Between Wait and Sleep in Java
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement Singly Linked List
New Features in Java 11
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Truyền giá trị và tham chiếu trong java
Cachable Static Assets with Spring MVC
Java IO vs NIO
Encode a String to UTF-8 in Java
Java Program to Solve TSP Using Minimum Spanning Trees
Deploy a Spring Boot WAR into a Tomcat Server
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Implement Bit Array
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Represent Linear Equations in Matrix Form
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Từ khóa throw và throws trong Java
Spring Boot - Hystrix
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Java Program to Implement Binary Heap
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
List Interface trong Java
Java Program to Implement Fermat Factorization Algorithm
Java Program to Implement Shoelace Algorithm
Check If a String Is Numeric in Java