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:
Getting the Size of an Iterable in Java
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Upload and Display Excel Files with Spring MVC
Jackson Ignore Properties on Marshalling
Spring Security Form Login
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
The Difference Between Collection.stream().forEach() and Collection.forEach()
Spring’s RequestBody and ResponseBody Annotations
How to Store Duplicate Keys in a Map in Java?
Java Program to Check Multiplicability of Two Matrices
Hướng dẫn Java Design Pattern – Bridge
Java Program to Perform integer Partition for a Specific Case
Hashtable trong java
Performance Difference Between save() and saveAll() in Spring Data
Guide to Guava Multimap
Java Program to Implement Binomial Heap
Đồng bộ hóa các luồng trong Java
Java Program to implement Priority Queue
Java Map With Case-Insensitive Keys
Quick Guide to java.lang.System
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Convert String to int or Integer in Java
A Custom Media Type for a Spring REST API
Using Spring ResponseEntity to Manipulate the HTTP Response
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Implement vector
The Difference Between map() and flatMap()
Mệnh đề if-else trong java
Hamcrest Collections Cookbook
Java Program to Implement Shell Sort
Introduction to the Functional Web Framework in Spring 5