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:
Convert Hex to ASCII in Java
Guide to Spring Cloud Kubernetes
Java Program to Find Path Between Two Nodes in a Graph
Java Program to Implement Interpolation Search Algorithm
Serve Static Resources with Spring
Java Program to Implement Gauss Jordan Elimination
Java Program to Implement Bresenham Line Algorithm
Spring Data – CrudRepository save() Method
Java Program to add two large numbers using Linked List
Using JWT with Spring Security OAuth
Setting the Java Version in Maven
SOAP Web service: Authentication trong JAX-WS
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
A Guide to BitSet in Java
Hướng dẫn sử dụng lớp Console trong java
New Features in Java 8
Receive email using POP3
Java Program to Implement DelayQueue API
Guide to the Java Queue Interface
Creating a Generic Array in Java
Creating a Custom Starter with Spring Boot
Introduction to the Java ArrayDeque
Configuring a DataSource Programmatically in Spring Boot
Jackson – Decide What Fields Get Serialized/Deserialized
A Quick Guide to Using Keycloak with Spring Boot
Sắp xếp trong Java 8
Java Program to Implement Radix Sort
The DAO with Spring and Hibernate
New Features in Java 9
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Converting a Stack Trace to a String in Java
Working With Maps Using Streams