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:
How to Set TLS Version in Apache HttpClient
Bootstrapping Hibernate 5 with Spring
Java Program to Implement Park-Miller Random Number Generation Algorithm
Generating Random Dates in Java
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Why String is Immutable in Java?
A Quick Guide to Spring MVC Matrix Variables
Java Program to Implement LinkedHashSet API
JUnit 5 for Kotlin Developers
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Iterable to Stream in Java
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Implement Miller Rabin Primality Test Algorithm
Java Program to Find the Connected Components of an UnDirected Graph
New Features in Java 8
Java Program to Find a Good Feedback Edge Set in a Graph
An Introduction to Java.util.Hashtable Class
The Registration Process With Spring Security
How to Break from Java Stream forEach
How to Replace Many if Statements in Java
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Summing Numbers with Java Streams
Converting Between a List and a Set in Java
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Guide to BufferedReader
Java Program to Implement CopyOnWriteArrayList API
Java Program to Implement Control Table
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Tiêu chuẩn coding trong Java (Coding Standards)
Introduction to Spring Data REST
Introduction to Spliterator in Java
Guide to Apache Commons CircularFifoQueue