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:
Các nguyên lý thiết kế hướng đối tượng – SOLID
Guide to PriorityBlockingQueue in Java
An Intro to Spring Cloud Vault
Ép kiểu trong Java (Type casting)
New Features in Java 8
An Introduction to Java.util.Hashtable Class
Java Program to Solve any Linear Equation in One Variable
Request a Delivery / Read Receipt in Javamail
Java Program to Implement Nth Root Algorithm
Java Program to Implement Levenshtein Distance Computing Algorithm
Java Program to Implement Rolling Hash
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Spring Boot - Quick Start
Intro to Spring Boot Starters
Lớp Properties trong java
Lấy ngày giờ hiện tại trong Java
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Implement Booth Algorithm
Java – Delete a File
Optional trong Java 8
Java Program to Describe the Representation of Graph using Incidence Matrix
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
How to Implement Caching using Adonis.js 5
Java Program to Implement the MD5 Algorithm
Java String Conversions
Java Program to Implement Control Table
New Features in Java 9
Introduction to Spring Method Security
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Handling Errors in Spring WebFlux
Assertions in JUnit 4 and JUnit 5
Guide to Java 8’s Collectors