-
Notifications
You must be signed in to change notification settings - Fork 2
/
Linear_Congruential_PRNG.cpp
62 lines (56 loc) · 1.01 KB
/
Linear_Congruential_PRNG.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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 << "10 random numbers:" << endl ;
int a[10];
for (int x = 0; x < 10; x++) {
a[x] = ms_rnd.rnd();
cout << a[x] << endl;
}
int m, n, j, r;
int sum = 0;
for (j = 0; j < 10; j += 2)
{
m = a[j];
n = a[j + 1];
do
{
r = m%n;
m = n;
n = r;
} while (r != 0);
if (m == 1)
sum = sum + 1;
}
double Pi, p;
p = 30.0 / sum;
Pi = sqrt(p);
cout << "Estimate value of Pi is: " << Pi << "\n";
system("pause");
return 0;
}