C++ Program to Implement Linear Congruential Method for Generating Pseudo Random Number

«
»
This is a C++ Program to generate random numbers using Linear Congruential Generator. A linear congruential generator (LCG) is an algorithm that yields a sequence of pseudo-randomized numbers calculated with a discontinuous piecewise linear equation. The method represents one of the oldest and best-known pseudorandom number generator algorithms. The theory behind them is relatively easy to understand, and they are easily implemented and fast, especially on computer hardware which can provide modulo arithmetic by storage-bit truncation.

Here is source code of the C++ Program to Implement the linear congruential generator for Pseudo Random Number Generation. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class mRND
  6. {
  7.     public:
  8.         void seed(unsigned int s)
  9.         {
  10.             _seed = s;
  11.         }
  12.  
  13.     protected:
  14.         mRND() :
  15.             _seed(0), _a(0), _c(0), _m(2147483648)
  16.         {
  17.         }
  18.         int rnd()
  19.         {
  20.             return (_seed = (_a * _seed + _c) % _m);
  21.         }
  22.  
  23.         int _a, _c;
  24.         unsigned int _m, _seed;
  25. };
  26.  
  27. class MS_RND: public mRND
  28. {
  29.     public:
  30.         MS_RND()
  31.         {
  32.             _a = 214013;
  33.             _c = 2531011;
  34.         }
  35.         int rnd()
  36.         {
  37.             return mRND::rnd() >> 16;
  38.         }
  39. };
  40.  
  41. class BSD_RND: public mRND
  42. {
  43.     public:
  44.         BSD_RND()
  45.         {
  46.             _a = 1103515245;
  47.             _c = 12345;
  48.         }
  49.         int rnd()
  50.         {
  51.             return mRND::rnd();
  52.         }
  53. };
  54.  
  55. int main(int argc, char* argv[])
  56. {
  57.     BSD_RND bsd_rnd;
  58.     MS_RND ms_rnd;
  59.  
  60.     cout << "MS RAND:" << endl << "========" << endl;
  61.     for (int x = 0; x < 10; x++)
  62.         cout << ms_rnd.rnd() << endl;
  63.  
  64.     cout << endl << "BSD RAND:" << endl << "=========" << endl;
  65.     for (int x = 0; x < 10; x++)
  66.         cout << bsd_rnd.rnd() << endl;
  67.  
  68.     return 0;
  69. }

Output:

$ g++ LinearCongruentialGenerator.cpp
$ a.out
 
MS RAND:
========
38
7719
21238
2437
8855
11797
8365
32285
10450
30612
 
BSD RAND:
=========
12345
1406932606
654583775
1449466924
229283573
1109335178
1051550459
1293799192
794471793
551188310

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.