/* calculating pi by throwing stones */ #include ; #include ; #define max 1000 /* number of stones to be thrown */ #define seed 11168 /* seed for number generator */ FILE *output; /* internal filename */ main() { int i, pi=0; double x, y, area; output= fopen("pi.dat", "w"); /* external file name */ srand48(seed); /* seed the number generator */ for (i=1; i<=max; i++) { x= drand48()*2-1; /* creates floats between */ y= drand48()*2-1; /* 1 and -1 */ if ((x*x + y*y)<1) pi++; area=4.0*(double) pi/(double) i; /* calculate area */ fprintf(output, "%i\t%f\n", i, area); } fclose (output); }