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 Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Spring Boot - Exception Handling
Shuffling Collections In Java
Java Program to Implement Miller Rabin Primality Test Algorithm
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java – Reader to Byte Array
Java Program to Implement Min Hash
Java Program to Implement the MD5 Algorithm
Class Loaders in Java
Guide to Dynamic Tests in Junit 5
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Notify User of Login From New Device or Location
Convert Hex to ASCII in Java
Java Program to Implement Meldable Heap
Guide to Java 8’s Collectors
Java Program to Implement Self organizing List
Beans and Dependency Injection
Spring Boot - Google OAuth2 Sign-In
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
New Features in Java 15
Hashtable trong java
Java Map With Case-Insensitive Keys
Request Method Not Supported (405) in Spring
Exception Handling in Java
Hướng dẫn sử dụng Lớp FilePermission trong java
A Guide to JPA with Spring
HashMap trong Java hoạt động như thế nào?
Spring WebClient vs. RestTemplate
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Date Time trong Java 8
Server-Sent Events in Spring