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:
Spring Security – Reset Your Password
Java Program to Implement Disjoint Sets
Giới thiệu thư viện Apache Commons Chain
Split a String in Java
Introduction to Spring Method Security
Java Program to Compute Determinant of a Matrix
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Send an email with an attachment
Find the Registered Spring Security Filters
Java Program to Construct a Random Graph by the Method of Random Edge Selection
JUnit 5 @Test Annotation
Merging Streams in Java
Java Program to Implement DelayQueue API
Java Program to Find kth Largest Element in a Sequence
Date Time trong Java 8
Integer Constant Pool trong Java
Introduction to Thread Pools in Java
Java Program to Implement Park-Miller Random Number Generation Algorithm
Logging a Reactive Sequence
Java Program to Perform Searching Using Self-Organizing Lists
Introduction to Spring Data JDBC
Java Program to Implement Binary Search Tree
Java – Combine Multiple Collections
Spring Boot - Runners
Configuring a DataSource Programmatically in Spring Boot
Spring Boot - Enabling Swagger2
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Java Streams vs Vavr Streams
Kết hợp Java Reflection và Java Annotations
Cài đặt và sử dụng Swagger UI
New Features in Java 13