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:
Service Registration with Eureka
Multi Dimensional ArrayList in Java
Default Password Encoder in Spring Security 5
Getting Started with Forms in Spring MVC
Lập trình đa luồng với CompletableFuture trong Java 8
Check if there is mail waiting
A Guide to Spring Cloud Netflix – Hystrix
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to Perform Complex Number Multiplication
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Logout in an OAuth Secured Application
Java Program to Implement a Binary Search Tree using Linked Lists
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Send an email with an attachment
Java Program to Implement Strassen Algorithm
Quick Guide to Spring Bean Scopes
Apache Commons Collections MapUtils
ETL with Spring Cloud Data Flow
Check if a String is a Palindrome in Java
JWT – Token-based Authentication trong Jersey 2.x
An Intro to Spring Cloud Zookeeper
Một số nguyên tắc, định luật trong lập trình
Jackson vs Gson
Redirect to Different Pages after Login with Spring Security
Java Program to Implement Hash Tree
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Hướng dẫn Java Design Pattern – Interpreter
Basic Authentication with the RestTemplate
Java Program to Solve any Linear Equation in One Variable
Limiting Query Results with JPA and Spring Data JPA
Spring Webflux and CORS
Java Program to Represent Graph Using Adjacency List