-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi-monte-carlo.cpp
54 lines (41 loc) · 1.56 KB
/
pi-monte-carlo.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
/* C++ program for estimation of Pi using Monte
Carlo Simulation */
#include <bits/stdc++.h>
// Defines precision for x and y values. More the
// interval, more the number of significant digits
#define INTERVAL 10000
using namespace std;
int main()
{
int interval, i;
double rand_x, rand_y, origin_dist, pi;
int circle_points = 0, square_points = 0;
// Initializing rand()
srand(time(NULL));
// Total Random numbers generated = possible x
// values * possible y values
for (i = 0; i < (INTERVAL * INTERVAL); i++) {
// Randomly generated x and y values
rand_x = double(rand() % (INTERVAL + 1)) / INTERVAL;
rand_y = double(rand() % (INTERVAL + 1)) / INTERVAL;
// Distance between (x, y) from the origin
origin_dist = rand_x * rand_x + rand_y * rand_y;
// Checking if (x, y) lies inside the define
// circle with R=1
if (origin_dist <= 1)
circle_points++;
// Total number of points generated
square_points++;
// estimated pi after this iteration
pi = double(4 * circle_points) / square_points;
// For visual understanding (Optional)
cout << rand_x << " " << rand_y << " " << circle_points
<< " " << square_points << " - " << pi << endl << endl;
// Pausing estimation for first 10 values (Optional)
if (i < 20)
getchar();
}
// Final Estimated Value
cout << "\nFinal Estimation of Pi = " << pi;
return 0;
}