Java Program to Generate Random Numbers Using Multiply with Carry Method

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:

Spring RestTemplate Request/Response Logging
Java Program to Compare Binary and Sequential Search
Java Program to Implement Sorted Array
Java Program to Implement ArrayList API
Check If Two Lists are Equal in Java
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Perform Right Rotation on a Binary Search Tree
Vector trong Java
An Example of Load Balancing with Zuul and Eureka
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Map Serialization and Deserialization with Jackson
Java Program to Implement Warshall Algorithm
Java Program to Implement the String Search Algorithm for Short Text Sizes
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Converting a List to String in Java
Java Program to Implement Binary Tree
Finding the Differences Between Two Lists in Java
Java Program to Create the Prufer Code for a Tree
Java Program to Implement Insertion Sort
Logging in Spring Boot
Java Program to Implement Sorted Vector
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Guide to Guava Multimap
Giới thiệu Aspect Oriented Programming (AOP)
A Custom Data Binder in Spring MVC
Setting the Java Version in Maven
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Implement Queue
Java Program to Check Cycle in a Graph using Topological Sort
Exploring the Spring 5 WebFlux URL Matching
Java Program to Implement Dijkstra’s Algorithm using Queue
Spring Data MongoDB – Indexes, Annotations and Converters