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:
Using a Spring Cloud App Starter
Spring 5 Testing with @EnabledIf Annotation
Mockito and JUnit 5 – Using ExtendWith
Guide to BufferedReader
Java Timer
Arrays.asList vs new ArrayList(Arrays.asList())
Composition, Aggregation, and Association in Java
How to Convert List to Map in Java
Java Program to Implement Sorted List
Programmatic Transaction Management in Spring
Using a List of Values in a JdbcTemplate IN Clause
Different Ways to Capture Java Heap Dumps
Chuyển đổi giữa các kiểu dữ liệu trong Java
Mảng (Array) trong Java
The “final” Keyword in Java
Java Program to Implement Interpolation Search Algorithm
Getting Started with GraphQL and Spring Boot
Command-Line Arguments in Java
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Jackson Exceptions – Problems and Solutions
Hướng dẫn Java Design Pattern – Proxy
Hướng dẫn sử dụng Java Generics
Java Program to Implement Binomial Tree
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Create Java Applet to Simulate Any Sorting Technique
Java Program to Implement Borwein Algorithm
Hướng dẫn Java Design Pattern – Dependency Injection
Java List UnsupportedOperationException
Send an email with an attachment
JPA/Hibernate Persistence Context
Adding Shutdown Hooks for JVM Applications
Send email with authentication