Java Program to Generate Random Numbers Using Multiply with Carry Method
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.
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input : Length = 5 Output: 1581 16 1932 1969 1384 Input : Length = 2 Output: 985 3568
x(n) = (a*x(n-r) + c(n-1))mod n
c(n) = (a*x(n-r) + c(n-1))/n
- a is any multiplier,
- m is modulus,
- c is carry, and
- r is a initial number of seeds.
The random number sequence is: 508 1021 1549 857 414 1187 1425 1557 1945 1922