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:
Introduction to Java Serialization
HandlerAdapters in Spring MVC
Spring Security Logout
A Custom Media Type for a Spring REST API
Prevent Brute Force Authentication Attempts with Spring Security
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program for Douglas-Peucker Algorithm Implementation
Java Program to Implement Cartesian Tree
String Joiner trong Java 8
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Uploading MultipartFile with Spring RestTemplate
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Registration – Activate a New Account by Email
Most commonly used String methods in Java
Lớp HashMap trong Java
Getting Started with Forms in Spring MVC
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
REST Web service: Basic Authentication trong Jersey 2.x
Hướng dẫn Java Design Pattern – Service Locator
Quick Guide to Spring Bean Scopes
Spring Boot - Rest Template
Introduction to Using FreeMarker in Spring MVC
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
The Modulo Operator in Java
Spring Boot - Runners
Java Program to Implement TreeMap API
Java Program to Implement Circular Doubly Linked List
Getting Started with GraphQL and Spring Boot
Intro to Inversion of Control and Dependency Injection with Spring