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:
HttpClient with SSL
Spring Security 5 – OAuth2 Login
ETL with Spring Cloud Data Flow
Java Program to Check Whether Graph is DAG
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Implement Find all Forward Edges in a Graph
JUnit5 @RunWith
Java Program to Implement CopyOnWriteArraySet API
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Logout in an OAuth Secured Application
Java Program to Implement the Monoalphabetic Cypher
Java Program to Implement Gale Shapley Algorithm
Introduction to the Java NIO2 File API
Java Program to Check whether Directed Graph is Connected using BFS
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Function trong Java 8
Java Program to Compute Determinant of a Matrix
Java Program to Implement Aho-Corasick Algorithm for String Matching
Hướng dẫn Java Design Pattern – Chain of Responsibility
HttpClient Connection Management
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Checked and Unchecked Exceptions in Java
Running Spring Boot Applications With Minikube
New Stream Collectors in Java 9
Guide to ThreadLocalRandom in Java
Java Program to implement Sparse Vector
Spring MVC Content Negotiation
Guide to Java Instrumentation
Introduction to Spring Data REST
Simple Single Sign-On with Spring Security OAuth2
Hướng dẫn Java Design Pattern – Observer
Exception Handling in Java