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
Send email with JavaMail
Java Program to Implement Iterative Deepening
Hướng dẫn Java Design Pattern – Factory Method
Java Program to Implement Hash Tables Chaining with List Heads
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
A Guide to the finalize Method in Java
HttpAsyncClient Tutorial
Spring WebClient Filters
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Guide to DelayQueue
Hướng dẫn Java Design Pattern – Transfer Object
Java Program to Implement Control Table
Comparing Strings in Java
Java Program to Solve any Linear Equation in One Variable
Java Program to Implement Adjacency Matrix
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Java Program to Check Whether a Given Point is in a Given Polygon
String Operations with Java Streams
Pagination and Sorting using Spring Data JPA
Java Program to Perform Left Rotation on a Binary Search Tree
Zipping Collections in Java
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Sorting Query Results with Spring Data
Java Program to Implement Knapsack Algorithm
Java Program to Print only Odd Numbered Levels of a Tree
Lớp HashMap trong Java
Configure a RestTemplate with RestTemplateBuilder
Jackson – Marshall String to JsonNode
Consumer trong Java 8
Exploring the New Spring Cloud Gateway
Java Program to Implement Adjacency List