This is the java implementation of Multiply With Carry (MWC) algorithm to generate a set of random numbers. The main advantages of the MWC method are that it invokes simple computer integer arithmetic and leads to very fast generation of sequences of random numbers with immense periods. The formula it uses is, x(n) = (a*x(n-r) + c(n-1))mod n and c(n) = (a*x(n-r) + c(n-1))/n, where a is any multiplier, m is modulus, c is carry and r is initial number of seeds.
Here is the source code of the Java Program to Generate Random Numbers Using Multiply with Carry Method. 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 a random numbers based on Multiply with carry method
import java.util.Random;
public class Multiply_With_Carry
{
public static void main(String args[])
{
int max_Sequence_Elements = 10;
Random random = new Random();
int base_b = 2000;
int multiplier_a = random.nextInt(base_b);
int r = 1;
int []c = new int[max_Sequence_Elements];
int []x = new int[max_Sequence_Elements];
c[0] = random.nextInt(multiplier_a);
x[0] = random.nextInt(base_b);
System.out.print("The random number sequence is: " + x[0]);
//generating sequence
for(int i=1; i<max_Sequence_Elements; i++)
{
x[i] = (multiplier_a*x[i-r] + c[i-1]) % base_b;
c[i] = (multiplier_a*x[i-r] + c[i-1]) / base_b;
System.out.print(" " + x[i]);
}
}
}
Output:
$ javac Multiply_With_Carry.java $ java Multiply_With_Carry The random number sequence is: 795 382 1487 260 475 1798 1347 722 1389 63
Related posts:
Sorting in Java
Adding Parameters to HttpClient Requests
Spring Boot Configuration with Jasypt
HashSet trong java
Java List UnsupportedOperationException
Java Program to Implement Ternary Search Algorithm
Java – Write an InputStream to a File
Java Program to Implement Hash Tables with Quadratic Probing
Summing Numbers with Java Streams
Java Program to Implement Quick Sort with Given Complexity Constraint
HandlerAdapters in Spring MVC
Apache Commons Collections BidiMap
Sorting Query Results with Spring Data
How to Define a Spring Boot Filter?
Jackson Exceptions – Problems and Solutions
Quick Guide to Spring MVC with Velocity
Chương trình Java đầu tiên
Introduction to the Java NIO Selector
Spring REST API + OAuth2 + Angular
Java Program to Find a Good Feedback Vertex Set
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Spring Boot - Servlet Filter
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Versioning a REST API
How to Return 404 with Spring WebFlux
Compare Two JSON Objects with Jackson
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Implement RoleUnresolvedList API
Using a Spring Cloud App Starter
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement Euler Circuit Problem
Java Program to Generate Date Between Given Range