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:
HashMap trong Java hoạt động như thế nào?
Spring Cloud – Bootstrapping
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Implement Direct Addressing Tables
Object Type Casting in Java
Spring Security and OpenID Connect
How to Read HTTP Headers in Spring REST Controllers
Lập trình đa luồng với Callable và Future trong Java
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Generating Random Dates in Java
How to Iterate Over a Stream With Indices
Hướng dẫn Java Design Pattern – Mediator
Performance Difference Between save() and saveAll() in Spring Data
Spring Cloud – Adding Angular
Programmatic Transaction Management in Spring
A Guide to System.exit()
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Remove the First Element from a List
Shuffling Collections In Java
Database Migrations with Flyway
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java – String to Reader
Java InputStream to Byte Array and ByteBuffer
Spring Data JPA @Modifying Annotation
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Extract network card address
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Compute Determinant of a Matrix
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Implement HashTable API