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:
Java Program to Find the Vertex Connectivity of a Graph
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Spring Data MongoDB Transactions
Java Program to Implement Horner Algorithm
Java Program to Implement Levenshtein Distance Computing Algorithm
Java Program to Implement Sorted Circular Doubly Linked List
Java 14 Record Keyword
Java 8 – Powerful Comparison with Lambdas
Add Multiple Items to an Java ArrayList
Bootstrap a Web Application with Spring 5
Adding a Newline Character to a String in Java
Cài đặt và sử dụng Swagger UI
Custom Thread Pools In Java 8 Parallel Streams
Java Program to Generate Random Numbers Using Multiply with Carry Method
REST Web service: Basic Authentication trong Jersey 2.x
Java Program to Implement Sorted Doubly Linked List
Java Program to Implement Stack
How to Get the Last Element of a Stream in Java?
HttpClient Timeout
Circular Dependencies in Spring
Fixing 401s with CORS Preflights and Spring Security
The Registration API becomes RESTful
Error Handling for REST with Spring
A Guide to System.exit()
Interface trong Java 8 – Default method và Static method
Binary Numbers in Java
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Một số nguyên tắc, định luật trong lập trình
ArrayList trong java
The DAO with JPA and Spring
How to Replace Many if Statements in Java
Java – Rename or Move a File