#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main() {
double x, pi;
double p=0.0, p2=0.0, e=0.0; //p=f(r),p2=f^2,e=error
int try, ntry;
printf("Input the number of MC trials\n");
scanf("%d",&ntry);
srand((unsigned)time((long *)0));
for (try=0; try<ntry; try++) {
x = rand()/(double)RAND_MAX;
p += 4.0/(1.0 + x*x);
p2 += 4.0* 4.0/((1.0 + x*x)*(1.0 + x*x));
}
pi = p/ntry;
p2 = p2/ntry;
e = sqrt((p2-pi*pi)/((double)(ntry-1)));
printf("MC estimate for PI = %f\n Error = %e\n",pi,e);
return 0;
}