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:
OAuth2.0 and Dynamic Client Registration
Java Program to Perform Deletion in a BST
Java Program to Implement Counting Sort
Guide to the Fork/Join Framework in Java
Java Program to Implement D-ary-Heap
The Basics of Java Security
Spring Boot - Zuul Proxy Server and Routing
Logging in Spring Boot
Tránh lỗi NullPointerException trong Java như thế nào?
Migrating from JUnit 4 to JUnit 5
Using Spring @ResponseStatus to Set HTTP Status Code
Java Program to Solve a Matching Problem for a Given Specific Case
Java – String to Reader
Java Program to Perform Search in a BST
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Jackson Date
JUnit5 @RunWith
Fixing 401s with CORS Preflights and Spring Security
Java Program to Find a Good Feedback Edge Set in a Graph
Mapping Nested Values with Jackson
Java Program to Find a Good Feedback Vertex Set
Collect a Java Stream to an Immutable Collection
Exploring the Spring Boot TestRestTemplate
Java Program to Construct an Expression Tree for an Prefix Expression
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Java Program to Implement Booth Algorithm
New Features in Java 9
Sort a HashMap in Java
How to Use if/else Logic in Java 8 Streams
JPA/Hibernate Persistence Context
Create a Custom Auto-Configuration with Spring Boot
@Order in Spring