-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi.cpp
37 lines (30 loc) · 1.05 KB
/
pi.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
#include "rtweekend.h"
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
void estimatePI() {
int inside_circle = 0;
int inside_circle_stratified = 0;
int sqrt_N = 10000;
std::cout << std::fixed << std::setprecision(12);
for (int i = 0; i < sqrt_N; ++i) {
for (int j = 0; j < sqrt_N; ++j) {
auto x = random_double(-1, 1);
auto y = random_double(-1, 1);
if (x * x + y * y < 1)
inside_circle++;
x = 2 * ((i + random_double()) / sqrt_N) - 1;
y = 2 * ((j + random_double()) / sqrt_N) - 1;
if (x * x + y * y < 1)
inside_circle_stratified++;
}
}
auto N = static_cast<double>(sqrt_N) * sqrt_N;
std::cout << std::fixed << std::setprecision(12);
std::cout
<< "Regular Estimate of Pi = "
<< 4*double(inside_circle) / (sqrt_N*sqrt_N) << '\n'
<< "Stratified Estimate of Pi = "
<< 4*double(inside_circle_stratified) / (sqrt_N*sqrt_N) << '\n';
}