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:
The Modulo Operator in Java
So sánh HashMap và Hashtable trong Java
Spring AMQP in Reactive Applications
List Interface trong Java
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
A Custom Data Binder in Spring MVC
Batch Processing with Spring Cloud Data Flow
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Implement Weight Balanced Tree
Java Program to Find kth Largest Element in a Sequence
Java Program to implement Array Deque
Spring Boot - Internationalization
Configure a RestTemplate with RestTemplateBuilder
Java Program to Solve the 0-1 Knapsack Problem
Creating a Web Application with Spring 5
JUnit 5 for Kotlin Developers
How to Convert List to Map in Java
HashSet trong java
Upload and Display Excel Files with Spring MVC
Java Program to Implement Hash Tree
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Exploring the Spring Boot TestRestTemplate
How to Round a Number to N Decimal Places in Java
Generic Constructors in Java
Một số nguyên tắc, định luật trong lập trình
Java Program to Implement Min Hash
Java Program to Implement Hash Tables with Double Hashing
A Guide to Apache Commons Collections CollectionUtils
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Sử dụng CountDownLatch trong Java
Java Program to Implement Insertion Sort