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 the Checksum Method for Small String Messages and Detect
How to Define a Spring Boot Filter?
Tính kế thừa (Inheritance) trong java
Java Program to Implement Iterative Deepening
Spring Boot: Customize Whitelabel Error Page
The Thread.join() Method in Java
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java Program to Implement Shoelace Algorithm
Java Program to Perform Uniform Binary Search
Java Program to Implement Heap Sort Using Library Functions
Using the Not Operator in If Conditions in Java
Comparing Arrays in Java
Comparing Long Values in Java
Kết hợp Java Reflection và Java Annotations
Extract network card address
The Registration API becomes RESTful
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Hướng dẫn Java Design Pattern – Iterator
Java Program to Implement Double Ended Queue
Java Program to Create a Balanced Binary Tree of the Incoming Data
Java Program to Implement AttributeList API
Java Program to Encode a Message Using Playfair Cipher
Circular Dependencies in Spring
Sending Emails with Java
Java Multi-line String
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Hướng dẫn sử dụng Java Annotation
Java Program to Find kth Largest Element in a Sequence
An Introduction to Java.util.Hashtable Class
Comparing Dates in Java
A Guide to Java HashMap
New Features in Java 8