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:
A Guide to Java 9 Modularity
Java Program to Implement the Bin Packing Algorithm
Spring Security 5 – OAuth2 Login
Java Program to Implement VList
Lập trình đa luồng trong Java (Java Multi-threading)
Java Program to Find the GCD and LCM of two Numbers
Java – InputStream to Reader
Abstract class và Interface trong Java
Using Java Assertions
Java – String to Reader
Extra Login Fields with Spring Security
REST Pagination in Spring
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Get the workstation name or IP
How to use the Spring FactoryBean?
Java Program to Implement Stack
Notify User of Login From New Device or Location
Apache Camel with Spring Boot
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Spring Boot Actuator
Spring Cloud Series – The Gateway Pattern
Apache Commons Collections MapUtils
Send email with JavaMail
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement CopyOnWriteArrayList API
A Guide to HashSet in Java
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Java Program to Solve TSP Using Minimum Spanning Trees
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Convenience Factory Methods for Collections
Java Program to Implement Quick Sort Using Randomization
Hướng dẫn sử dụng luồng vào ra ký tự trong Java