Saturday 18 November 2017

C++ Program to Implement the linear congruential generator for Pseudo Random Number Generation


Code:

#include    iostream

using namespace std;

class mRND
{
    public:
        void seed(unsigned int s)
        {
            _seed = s;
        }

    protected:
        mRND() :
            _seed(0), _a(0), _c(0), _m(2147483648)
        {
        }
        int rnd()
        {
            return (_seed = (_a * _seed + _c) % _m);
        }

        int _a, _c;
        unsigned int _m, _seed;
};

class MS_RND: public mRND
{
    public:
        MS_RND()
        {
            _a = 214013;
            _c = 2531011;
        }
        int rnd()
        {
            return mRND::rnd() >> 16;
        }
};

class BSD_RND: public mRND
{
    public:
        BSD_RND()
        {
            _a = 1103515245;
            _c = 12345;
        }
        int rnd()
        {
            return mRND::rnd();
        }
};

int main(int argc, char* argv[])
{
    BSD_RND bsd_rnd;
    MS_RND ms_rnd;

    cout << "MS RAND:" << endl << "========" << endl;
    for (int x = 0; x < 10; x++)
        cout << ms_rnd.rnd() << endl;

    cout << endl << "BSD RAND:" << endl << "=========" << endl;
    for (int x = 0; x < 10; x++)
        cout << bsd_rnd.rnd() << endl;

    return 0;
}


Output:

MS RAND:
========
38
7719
21238
2437
8855
11797
8365
32285
10450
30612

BSD RAND:
=========
12345
1406932606
654583775
1449466924
229283573
1109335178
1051550459
1293799192
794471793
551188310


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