Program
/* calculating pi by throwing stones */
#include <stdio.h>
#include <math.h>
#define max 2000 /* number of stones to be thrown */
#define seed 11168 /* seed for number generator */
FILE *output; /* internal file name*/
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++; /* stone hit the pond */
area=4.0*(double) pi/(double) i; /* calculate area */
fprintf(output, "%i\t%f\n", i, area);
}
fclose (output);
}
A DEC C source and a ANSI-C source
which you can save and run on your computer.
Back to main document.