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:
Introduction to Java 8 Streams
Java Program to Perform Cryptography Using Transposition Technique
Working with Kotlin and JPA
Phân biệt JVM, JRE, JDK
Intersection of Two Lists in Java
Một số ký tự đặc biệt trong Java
Jackson Unmarshalling JSON with Unknown Properties
Java Program to Implement Hopcroft Algorithm
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Guide to the Java ArrayList
Java Program to Implement Randomized Binary Search Tree
Toán tử trong java
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Connect through a Proxy
Java Program to Implement Hash Tables Chaining with Binary Trees
Prevent Brute Force Authentication Attempts with Spring Security
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Implement K Way Merge Algorithm
Error Handling for REST with Spring
A Guide to the ResourceBundle
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement Find all Forward Edges in a Graph
Java Program to Implement Sparse Array
Java Program to Implement Triply Linked List
Java Program to Solve any Linear Equation in One Variable
Spring Boot Security Auto-Configuration
Hướng dẫn Java Design Pattern – Mediator
Introduction to Eclipse Collections
Hướng dẫn sử dụng Java Annotation
OAuth2 Remember Me with Refresh Token