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 Define a Spring Boot Filter?
Java Program to Implement Graph Coloring Algorithm
Guide to the Synchronized Keyword in Java
Java equals() and hashCode() Contracts
Java Program to Find the Minimum value of Binary Search Tree
Java Program to Implement Min Heap
Giới thiệu Java 8
How to Get All Dates Between Two Dates?
Từ khóa throw và throws trong Java
HttpClient 4 – Follow Redirects for POST
Java Copy Constructor
Removing all duplicates from a List in Java
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Tính đa hình (Polymorphism) trong Java
Java Program to Implement vector
Spring Data Java 8 Support
Guide to java.util.concurrent.BlockingQueue
Java Program to Find the Vertex Connectivity of a Graph
Java Program to Implement Suffix Tree
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Java Program to Perform Search in a BST
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Tính đóng gói (Encapsulation) trong java
Bootstrap a Web Application with Spring 5
Using a List of Values in a JdbcTemplate IN Clause
Java Program for Douglas-Peucker Algorithm Implementation
Lập trình đa luồng trong Java (Java Multi-threading)
Java Program to Implement ArrayDeque API
Java Program to Implement Hash Tables with Quadratic Probing
Java Program to Implement ConcurrentHashMap API
A Guide to Java 9 Modularity
A Guide to WatchService in Java NIO2