Skip to content

Instantly share code, notes, and snippets.

@Toruhs
Created February 1, 2012 17:09
Show Gist options
  • Save Toruhs/1718076 to your computer and use it in GitHub Desktop.
Save Toruhs/1718076 to your computer and use it in GitHub Desktop.
Monte Carlo Method
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
double area, pi, x, y, n, N, i;
n = 0;
N = 100000000;
srand((unsigned)time(NULL));
for(i=0;i<N;i++)
{
x = rand() / (double)RAND_MAX;
y = rand() / (double)RAND_MAX;
if((x*x + y*y) <= 1)
{
n++;
}
}
area = 1 * n / N;
pi = area * 4;
printf("%f\n",pi);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment