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:
Sử dụng CyclicBarrier trong Java
New Features in Java 9
A Guide to TreeSet in Java
Basic Authentication with the RestTemplate
Getting Started with Forms in Spring MVC
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
DistinctBy in the Java Stream API
Spring REST API + OAuth2 + Angular
Java Program to Generate Random Numbers Using Probability Distribution Function
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Java Program to Implement RoleList API
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
The DAO with Spring and Hibernate
Tính trừu tượng (Abstraction) trong Java
Different Ways to Capture Java Heap Dumps
Các kiểu dữ liệu trong java
Exploring the Spring 5 WebFlux URL Matching
Removing Elements from Java Collections
HandlerAdapters in Spring MVC
Guide to the ConcurrentSkipListMap
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Lớp Properties trong java
Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Perform Finite State Automaton based Search
Java Program to Perform Deletion in a BST
Transaction Propagation and Isolation in Spring @Transactional
Hướng dẫn Java Design Pattern – Command
Guide to the Java TransferQueue
Send email with authentication
Java Program to Implement TreeMap API
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
A Guide to BitSet in Java