forked from RayTracing/TheRestOfYourLife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi.cc
37 lines (34 loc) · 1.45 KB
/
pi.cc
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
//==================================================================================================
// Written in 2016 by Peter Shirley <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related and
// neighboring rights to this software to the public domain worldwide. This software is distributed
// without any warranty.
//
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==================================================================================================
#include <math.h>
#include <stdlib.h>
#include <iostream>
int main() {
int inside_circle = 0;
int inside_circle_stratified = 0;
int sqrt_N = 30000;
for (int i = 0; i < sqrt_N; i++) {
for (int j = 0; j < sqrt_N; j++) {
float x = 2*drand48() - 1;
float y = 2*drand48() - 1;
if (x*x + y*y < 1)
inside_circle++;
x = 2*((i + drand48()) / sqrt_N) - 1;
y = 2*((j + drand48()) / sqrt_N) - 1;
if (x*x + y*y < 1)
inside_circle_stratified++;
}
}
std::cout << "Regular Estimate of Pi = " <<
4*float(inside_circle) / (sqrt_N*sqrt_N) << "\n";
std::cout << "Stratified Estimate of Pi = " <<
4*float(inside_circle_stratified) / (sqrt_N*sqrt_N) << "\n";
}