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:
Create a Custom Auto-Configuration with Spring Boot
A Guide to HashSet in Java
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java Program to Find Maximum Element in an Array using Binary Search
Getting Started with GraphQL and Spring Boot
Java IO vs NIO
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Introduction to Spring Data MongoDB
Java Program to Implement Bellman-Ford Algorithm
Generate Spring Boot REST Client with Swagger
Registration – Password Strength and Rules
Java String to InputStream
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Implement Counting Sort
Binary Numbers in Java
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Guide to the Synchronized Keyword in Java
Understanding Memory Leaks in Java
Instance Profile Credentials using Spring Cloud
Java Program to Implement Suffix Tree
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Simultaneous Spring WebClient Calls
Semaphore trong Java
Java Program for Douglas-Peucker Algorithm Implementation
Java Program to Implement Dijkstra’s Algorithm using Set
How to Get All Dates Between Two Dates?
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to Implement Binary Search Tree
Java Program to Implement Doubly Linked List
Merging Two Maps with Java 8
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Java Program to Find All Pairs Shortest Path