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 Check whether Graph is Biconnected
Spring Boot - Interceptor
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Implement PriorityQueue API
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
How to Iterate Over a Stream With Indices
Tính đa hình (Polymorphism) trong Java
Java Program to Perform integer Partition for a Specific Case
Query Entities by Dates and Times with Spring Data JPA
Java Program to Implement Johnson’s Algorithm
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement Max Heap
Java Program to Implement Adjacency Matrix
Java Program to Check whether Graph is a Bipartite using DFS
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Logout in an OAuth Secured Application
The “final” Keyword in Java
Using Spring ResponseEntity to Manipulate the HTTP Response
Java Program to Compute Discrete Fourier Transform Using Naive Approach
A Guide to ConcurrentMap
Flattening Nested Collections in Java
Display Auto-Configuration Report in Spring Boot
Spring Boot - Google Cloud Platform
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Implement Queue
Custom Cascading in Spring Data MongoDB
Java Program to Implement Interpolation Search Algorithm
Wrapper Classes in Java
Java Program to Compute Determinant of a Matrix
Java Program to Implement LinkedHashMap API
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Từ khóa throw và throws trong Java