This is a C++ 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 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, ranging from around 260 to 22000000.
Here is source code of the C++ Program to Generate Random Numbers Using Multiply with Carry Method. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
using namespace std;
int main(int argc, char **argv)
int max_Sequence_Elements = 10;
int base_b = 2000;
int multiplier_a = rand() % base_b;
int r = 1;
c = rand() % multiplier_a;
x = rand() % base_b;
cout << "The random number sequence is: " << x;
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;
cout << " " << x[i];
cout << "...";
$ g++ MultiplyWithCarry.cpp $ a.out The random number sequence is: 334 1711 157 472 1355 1564 151 223 1146 990...
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for C++ Internship
- Buy Programming Books
- Buy C++ Books
- Practice Computer Science MCQs
- Practice Programming MCQs