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 the Java NIO2 File API
Function trong Java 8
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Remove All Occurrences of a Specific Value from a List
Java Program to Implement Extended Euclid Algorithm
Documenting a Spring REST API Using OpenAPI 3.0
Java 8 and Infinite Streams
Java Program to Implement Levenshtein Distance Computing Algorithm
Giới thiệu về Stream API trong Java 8
Toán tử trong java
Allow user:password in URL
Introduction to Spring Cloud CLI
Java – Convert File to InputStream
Java Program to Implement Weight Balanced Tree
Guide to the Java Clock Class
Spring Data JPA and Null Parameters
Filtering and Transforming Collections in Guava
Map to String Conversion in Java
Class Loaders in Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java NIO2 Path API
Phân biệt JVM, JRE, JDK
Spring Boot - Google Cloud Platform
Java Program to Use rand and srand Functions
JWT – Token-based Authentication trong Jersey 2.x
Java – Get Random Item/Element From a List
Wiring in Spring: @Autowired, @Resource and @Inject
The DAO with JPA and Spring
Java Program to Generate a Random Subset by Coin Flipping
Guide to Spring @Autowired
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Find Whether a Path Exists Between 2 Given Nodes