Saturday, 18 November 2017

C++ Program to Generate Random Numbers Using Multiply with Carry Method


Code:

#include   iostream
#include   math.h
#include   stdlib.h

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;
    int c[max_Sequence_Elements];
    int x[max_Sequence_Elements];

    c[0] = rand() % multiplier_a;
    x[0] = rand() % base_b;

    cout << "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;
        cout << " " << x[i];
    }
    cout << "...";
}


Output:

The random number sequence is: 334 1711 157 472 1355 1564 151 223 1146 990...



More C++ Programs:















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...