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:
Spring Boot - Building RESTful Web Services
Java Program to Check if a Matrix is Invertible
Recommended Package Structure of a Spring Boot Project
Java – Delete a File
Extract network card address
Custom Thread Pools In Java 8 Parallel Streams
Spring @RequestParam Annotation
Java Program to Implement Gauss Seidel Method
Spring Boot - Admin Server
A Guide to JUnit 5 Extensions
Spring Security – security none, filters none, access permitAll
Spring Boot Application as a Service
Java Program to Implement PriorityBlockingQueue API
Spring Cloud Connectors and Heroku
Java Program to Implement LinkedHashSet API
Java Program to Implement Ternary Search Algorithm
Posting with HttpClient
Sử dụng CyclicBarrier trong Java
Spring Boot - Cloud Configuration Client
Java Program to Implement Euler Circuit Problem
Spring Boot with Multiple SQL Import Files
Allow user:password in URL
Spring Boot - Bootstrapping
Java Program to Implement VList
Hướng dẫn Java Design Pattern – Builder
Java Program to Implement AttributeList API
Most commonly used String methods in Java
Getting a File’s Mime Type in Java
Intro to Inversion of Control and Dependency Injection with Spring
Partition a List in Java
Spring MVC and the @ModelAttribute Annotation
Java Program for Topological Sorting in Graphs