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:
Check If Two Lists are Equal in Java
Spring Boot - Database Handling
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Sắp xếp trong Java 8
Sử dụng CyclicBarrier trong Java
Java Program to Check Multiplicability of Two Matrices
Java Program to Implement Borwein Algorithm
Java Program to Implement Fenwick Tree
Consuming RESTful Web Services
Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Implement Euler Circuit Problem
Java Program to Implement Skew Heap
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Represent Graph Using 2D Arrays
Java Program to implement Bit Matrix
Overflow and Underflow in Java
Guide to the Synchronized Keyword in Java
Java Program to Implement Self Balancing Binary Search Tree
Java Program to Implement CopyOnWriteArraySet API
What is a POJO Class?
Java Program to Implement Find all Back Edges in a Graph
Send email with JavaMail
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
A Guide to EnumMap
Spring Security Custom AuthenticationFailureHandler
Java Program to Implement Ternary Heap
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Các nguyên lý thiết kế hướng đối tượng – SOLID
Spring Data – CrudRepository save() Method