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:
SOAP Web service: Authentication trong JAX-WS
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Implement the Hill Cypher
Quick Guide to @RestClientTest in Spring Boot
Write/Read cookies using HTTP and Read a file from the internet
HTTP Authentification and CGI/Servlet
Spring Boot - Tracing Micro Service Logs
Shuffling Collections In Java
Queue và PriorityQueue trong Java
Guide to the ConcurrentSkipListMap
Introduction to Apache Commons Text
Hướng dẫn Java Design Pattern – Composite
Jackson Annotation Examples
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Perform Deletion in a BST
Using the Map.Entry Java Class
Spring Data JPA @Query
Java Program to Generate All Possible Combinations of a Given List of Numbers
Một số từ khóa trong Java
Java Program to Solve Knapsack Problem Using Dynamic Programming
Spring Boot - Sending Email
Spring Security and OpenID Connect
Basic Authentication with the RestTemplate
Tips for dealing with HTTP-related problems
Spring Boot: Customize Whitelabel Error Page
Connect through a Proxy
Guide to Spring 5 WebFlux
Java Program to Implement the String Search Algorithm for Short Text Sizes
Guide to Java OutputStream
Guide to DelayQueue
Spring Boot - Batch Service
Explain about URL and HTTPS protocol