Created
February 19, 2017 22:21
-
-
Save fionn/ed3622b8e7a46f2547ec136331116b20 to your computer and use it in GitHub Desktop.
Calculate π using Monte Carlo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
double x, y; | |
double N = 100000; | |
int c = 0; | |
for(int i = 0; i < N; i++) | |
{ | |
x = double(random()) / RAND_MAX; | |
y = double(random()) / RAND_MAX; | |
if(x * x + y * y <= 1.0) | |
c++; | |
} | |
cout << 4.0 * c / N << endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment