In computer science, multiply-with-carry (MWC) is a method invented by George Marsaglia for generating sequences of random integers based on an initial set from two to many thousands of randomly chosen seed values. The main advantage of the MWC method is that it invokes simple computer integer arithmetic and leads to the very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000.
Example:
Input : Length = 5 Output: 1581 16 1932 1969 1384 Input : Length = 2 Output: 985 3568
Formula used:
x(n) = (a*x(n-r) + c(n-1))mod n
c(n) = (a*x(n-r) + c(n-1))/n
where,
- a is any multiplier,
- m is modulus,
- c is carry, and
- r is a initial number of seeds.
Java
// Java Program to generate a random numbers // Based on Multiply with carry method import java.util.Random; public class Main { public static void main(String args[]) { int Maximum_Sequence_Elements = 10; Random random = new Random(); int Base = 2000; int Multiplier = random.nextInt(Base); int r = 1; int[] c = new int[Maximum_Sequence_Elements]; int[] x = new int[Maximum_Sequence_Elements]; c[0] = random.nextInt(Multiplier); x[0] = random.nextInt(Base); System.out.print("The random number sequence is: " + x[0]); // generating sequence for (int i = 1; i < Maximum_Sequence_Elements; i++) { x[i] = (Multiplier * x[i - r] + c[i - 1]) % Base; c[i] = (Multiplier * x[i - r] + c[i - 1]) / Base; System.out.print(" " + x[i]); } } } |
The random number sequence is: 508 1021 1549 857 414 1187 1425 1557 1945 1922
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Generate Random Numbers Using Middle Square Method in Java
- Random vs Secure Random numbers in Java
- How to Create a Random Graph Using Random Edge Generation in Java?
- Java Program to Generate Random Hexadecimal Bytes
- Program to generate random alphabets
- Program to generate random string in PHP
- PHP program to Generate the random number in the given range (min, max)
- How to generate random number in given range using JavaScript?
- Generate random String of given size in Java
- How do I generate random integers within a specific range in Java?
- How to Generate a Random Directed Acyclic Graph for a Given Number of Edges in Java?
- How to generate a random color for a Matplotlib plot in Python?
- Java Program to Multiply two Matrices of any size
- BigDecimal multiply() Method in Java
- BigInteger multiply() Method in Java with Examples
- Java Program to Implement Sieve of Eratosthenes to Generate Prime Numbers Between Given Range
- Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
- Different Ways to Generate String by using Characters and Numbers in Java
- Generating random numbers in Java
- Java Program to Implement Naor-Reingold Pseudo Random Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

