Saturday 18 November 2017

C++ Program to Implement Park-Miller Random Number Generation Algorithm


Code:

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

using namespace std;

const long m = 2147483647L;
const long a = 48271L;
const long q = 44488L;
const long r = 3399L;

static long r_seed = 12345678L;

double uniform()
{
    long hi = r_seed / q;
    long lo = r_seed - q * hi;
    long t = a * lo - r * hi;
    if (t > 0)
        r_seed = t;
    else
        r_seed = t + m;
    return r_seed;
}

int main(int argc, char **argv)
{
    double A[10];

    for (int i = 0; i < 10; i++)
        A[i] = uniform();

    cout<<"Random numbers are:\n";
    for (int i = 0; i < 10; i++)
        cout << A[i]<
}


Output:

Random numbers are:
1.08525e+009
5.0826e+008
1.35229e+009
1.56324e+009
8.90733e+008
1.81003e+009
1.50959e+009
8.62973e+008
1.85299e+009
6.77684e+008



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...