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 Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Removing Elements from Java Collections
Java – File to Reader
Injecting Prototype Beans into a Singleton Instance in Spring
Spring Security OAuth Login with WebFlux
Java Program to Solve a Matching Problem for a Given Specific Case
Spring Security Logout
Spring Cloud AWS – EC2
Build a REST API with Spring and Java Config
Java 8 Predicate Chain
The Basics of Java Security
Integer Constant Pool trong Java
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Number Formatting in Java
Hướng dẫn Java Design Pattern – Observer
Spring Cloud AWS – RDS
New Features in Java 9
Java – Write to File
Java Program to Perform Cryptography Using Transposition Technique
Hướng dẫn Java Design Pattern – MVC
REST Web service: Upload và Download file với Jersey 2.x
Thao tác với tập tin và thư mục trong Java
Serverless Functions with Spring Cloud Function
Java Program to Implement CopyOnWriteArrayList API
Introduction to Netflix Archaius with Spring Cloud
Exploring the Spring Boot TestRestTemplate
Introduction to Spring Cloud OpenFeign
Concrete Class in Java
What is Thread-Safety and How to Achieve it?
Java InputStream to String
Java Program to Implement Extended Euclid Algorithm
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)