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:
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Implement Suffix Tree
Using JWT with Spring Security OAuth
Tạo chương trình Java đầu tiên sử dụng Eclipse
Guide to CountDownLatch in Java
Java Program to Generate Random Numbers Using Multiply with Carry Method
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Spring REST API + OAuth2 + Angular
Enum trong java
Hướng dẫn Java Design Pattern – DAO
Zipping Collections in Java
JUnit5 @RunWith
Beans and Dependency Injection
How to Return 404 with Spring WebFlux
Feign – Tạo ứng dụng Java RESTful Client
Reversing a Linked List in Java
Java Program to Convert a Decimal Number to Binary Number using Stacks
Marker Interface trong Java
Truyền giá trị và tham chiếu trong java
Java Program to Implement RoleUnresolvedList API
Introduction to Spring Cloud Netflix – Eureka
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Implement Merge Sort Algorithm on Linked List
Spring Boot Configuration with Jasypt
Java – Reader to InputStream
Java Program to Implement Aho-Corasick Algorithm for String Matching
Adding a Newline Character to a String in Java
Initialize a HashMap in Java
Getting a File’s Mime Type in Java
Hướng dẫn Java Design Pattern – Composite