Skip to content

Instantly share code, notes, and snippets.

@fionn
Created February 19, 2017 22:21
Show Gist options
  • Save fionn/ed3622b8e7a46f2547ec136331116b20 to your computer and use it in GitHub Desktop.
Save fionn/ed3622b8e7a46f2547ec136331116b20 to your computer and use it in GitHub Desktop.
Calculate π using Monte Carlo
#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