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 a Good Feedback Vertex Set
Spring Security 5 – OAuth2 Login
Java Program to Implement HashSet API
LinkedHashSet trong Java hoạt động như thế nào?
Spring Boot - OAuth2 with JWT
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
How to Return 404 with Spring WebFlux
Java Program to Construct an Expression Tree for an Prefix Expression
Java Program to Implement the String Search Algorithm for Short Text Sizes
Redirect to Different Pages after Login with Spring Security
Java Switch Statement
How to Get the Last Element of a Stream in Java?
Java equals() and hashCode() Contracts
Working with Tree Model Nodes in Jackson
Java Program to Find Maximum Element in an Array using Binary Search
Convert String to int or Integer in Java
Spring Boot Integration Testing with Embedded MongoDB
Converting Between a List and a Set in Java
How to Manually Authenticate User with Spring Security
A Guide to the ViewResolver in Spring MVC
Java Program to Perform Searching in a 2-Dimension K-D Tree
Setting Up Swagger 2 with a Spring REST API
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Java Program to Compute DFT Coefficients Directly
Spring Boot - File Handling
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Implement Disjoint Set Data Structure
Convert String to Byte Array and Reverse in Java
Lập trình hướng đối tượng (OOPs) trong java