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 Find the Median of two Sorted Arrays using Binary Search Approach
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Creating a Generic Array in Java
Format ZonedDateTime to String
Setting the Java Version in Maven
Spring MVC Setup with Kotlin
Java 9 Stream API Improvements
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Compute the Area of a Triangle Using Determinants
Running Spring Boot Applications With Minikube
Hashing a Password in Java
ETags for REST with Spring
Hướng dẫn Java Design Pattern – Strategy
Java Program to Perform Finite State Automaton based Search
Converting Between an Array and a Set in Java
Tạo chương trình Java đầu tiên sử dụng Eclipse
Converting a List to String in Java
Java Program to Implement LinkedBlockingDeque API
Converting Strings to Enums in Java
Java Program to Check whether Graph is Biconnected
Java Program to Implement Doubly Linked List
Java Program to Implement TreeSet API
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement ArrayDeque API
Java Program to Implement Stack using Two Queues
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Create Java Applet to Simulate Any Sorting Technique
ETL with Spring Cloud Data Flow
Spring MVC Content Negotiation
Mệnh đề if-else trong java
How to Add a Single Element to a Stream