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:
JWT – Token-based Authentication trong Jersey 2.x
New Features in Java 8
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Getting the Size of an Iterable in Java
Converting String to Stream of chars
Java List UnsupportedOperationException
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement HashTable API
Java Program to Find Strongly Connected Components in Graphs
Runnable vs. Callable in Java
Java Program to Implement Fermat Factorization Algorithm
Spring Cloud – Bootstrapping
Java Program to Implement Levenshtein Distance Computing Algorithm
Spring Boot - Exception Handling
Chương trình Java đầu tiên
Cài đặt và sử dụng Swagger UI
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Find Path Between Two Nodes in a Graph
Spring’s RequestBody and ResponseBody Annotations
Custom Thread Pools In Java 8 Parallel Streams
Jackson Ignore Properties on Marshalling
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Implement Self organizing List
Guide to Guava Multimap
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Send an email using the SMTP protocol
Java Program to Implement Floyd-Warshall Algorithm
Get and Post Lists of Objects with RestTemplate
Toán tử trong java
Working with Tree Model Nodes in Jackson
Java Program to Implement LinkedTransferQueue API
Abstract class và Interface trong Java