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:
Control the Session with Spring Security
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Hướng dẫn sử dụng String Format trong Java
Hướng dẫn Java Design Pattern – MVC
Using JWT with Spring Security OAuth (legacy stack)
A Guide to Apache Commons Collections CollectionUtils
Map Serialization and Deserialization with Jackson
Java Program to Implement Bresenham Line Algorithm
Request a Delivery / Read Receipt in Javamail
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement Shell Sort
Java Program to Implement Sorted List
LinkedHashSet trong Java hoạt động như thế nào?
Serverless Functions with Spring Cloud Function
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Implement Adjacency Matrix
The Dining Philosophers Problem in Java
Split a String in Java
Lớp HashMap trong Java
Sử dụng CyclicBarrier trong Java
Spring Boot - Creating Docker Image
Java Program to Implement Rope
Java String Conversions
Object Type Casting in Java
Quick Guide to Spring Controllers
Validations for Enum Types
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Concrete Class in Java
Spring Boot - Apache Kafka
TreeSet và sử dụng Comparable, Comparator trong java
Merging Two Maps with Java 8
Constructor Injection in Spring with Lombok