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 Implement Binary Tree
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Getting Started with GraphQL and Spring Boot
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Cài đặt và sử dụng Swagger UI
Simple Single Sign-On with Spring Security OAuth2
Jackson Ignore Properties on Marshalling
Java Program to Implement Ternary Search Tree
Java – Byte Array to Reader
Guide to CopyOnWriteArrayList
Java Program to Implement Word Wrap Problem
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
A Guide to Concurrent Queues in Java
Java Program to Implement Sieve Of Sundaram
Java Program to Implement RenderingHints API
Guide to Java OutputStream
Introduction to Thread Pools in Java
Spring Cloud AWS – Messaging Support
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Reversing a Linked List in Java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
REST Pagination in Spring
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Java Program to Check whether Undirected Graph is Connected using DFS
Introduction to Spring Boot CLI
The Java 8 Stream API Tutorial
Spring – Injecting Collections
Converting a List to String in Java
Spring Security 5 – OAuth2 Login
Convert String to int or Integer in Java
Java Program to Implement Max-Flow Min-Cut Theorem
Disable DNS caching