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:
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program for Douglas-Peucker Algorithm Implementation
The XOR Operator in Java
Disable Spring Data Auto Configuration
Phương thức tham chiếu trong Java 8 – Method References
Adding a Newline Character to a String in Java
Spring Boot - Quick Start
Java Program to Implement Bresenham Line Algorithm
Bootstrapping Hibernate 5 with Spring
Java Program to Implement Bucket Sort
Concatenating Strings In Java
Spring Boot - Database Handling
The Difference Between map() and flatMap()
Đồng bộ hóa các luồng trong Java
Java Program to Implement ScapeGoat Tree
Why String is Immutable in Java?
Sorting in Java
Converting Between an Array and a Set in Java
Hướng dẫn Java Design Pattern – Decorator
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Find a Good Feedback Vertex Set
A Guide to TreeMap in Java
Guide to the Synchronized Keyword in Java
Guide to Spring @Autowired
Java Program to Implement Sieve Of Sundaram
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Guide to BufferedReader
Spring MVC and the @ModelAttribute Annotation
Java Program to Perform Uniform Binary Search
Reversing a Linked List in Java
Hamcrest Collections Cookbook
How to Read a Large File Efficiently with Java