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:
Get and Post Lists of Objects with RestTemplate
Spring Boot - Sending Email
Java Program to Solve any Linear Equation in One Variable
Java Program to Find Transitive Closure of a Graph
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Check whether Graph is a Bipartite using DFS
Registration – Password Strength and Rules
Spring Boot With H2 Database
Convert XML to JSON Using Jackson
Java Program to Construct an Expression Tree for an Infix Expression
Template Engines for Spring
The HttpMediaTypeNotAcceptableException in Spring MVC
Guide to Escaping Characters in Java RegExps
Java Program to Implement Fibonacci Heap
A Guide to the Java LinkedList
Java Program to Implement Red Black Tree
Spring Boot - Admin Client
Spring @RequestParam Annotation
New Features in Java 9
Jackson vs Gson
Setting a Request Timeout for a Spring REST API
Java Program to Implement Cartesian Tree
Registration – Activate a New Account by Email
Spring 5 Testing with @EnabledIf Annotation
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
HashSet trong Java hoạt động như thế nào?
Java Program to Implement Ternary Search Tree
Java Program to Implement Trie
Serialization và Deserialization trong java
Java Program to Implement Min Heap
Java Program to Implement Self Balancing Binary Search Tree
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java