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 Find the Edge Connectivity of a Graph
Spring Boot - Thymeleaf
Spring Security Registration – Resend Verification Email
Immutable Map Implementations in Java
OAuth2 Remember Me with Refresh Token
Spring Boot - Rest Template
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Getting Started with Custom Deserialization in Jackson
Java Program to Implement Regular Falsi Algorithm
Guide to Dynamic Tests in Junit 5
Copy a List to Another List in Java
Convert Hex to ASCII in Java
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
LIKE Queries in Spring JPA Repositories
Spring Boot - Eureka Server
Java Program to Implement LinkedBlockingDeque API
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Implementing a Binary Tree in Java
Changing Annotation Parameters At Runtime
Java Program to Implement Ternary Search Algorithm
Java toString() Method
Introduction to Java Serialization
Java Program to Check Cycle in a Graph using Topological Sort
The Dining Philosophers Problem in Java
A Guide to the Java ExecutorService
Spring Boot Tutorial – Bootstrap a Simple Application
Giới thiệu Java 8
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Control the Session with Spring Security
Spring Boot Integration Testing with Embedded MongoDB
Hướng dẫn sử dụng Java Generics