/***************************************************************************/ /* ____Demonstrates SPRNG use for Monte Carlo integration____ */ /* Compute pi using Monte Carlo integration. Random points are generated */ /* in a square of size 2. The value of pi is estimated as four times the */ /* the proportion of samples that fall within a circle of unit radius. */ /***************************************************************************/ #include #include #include #define SIMPLE_SPRNG /* simple interface */ #include "sprng.h" #define EXACT_PI 3.141592653589793238462643 int gtype; main(argc,argv) int argc; char *argv[]; { int in, n, in_old, n_old; double pi, error, stderror, p=EXACT_PI/4.0; char filename[80]; /*--- reading in a generator type ***************************************/ /* You may not need this. This is for generator type explanation. *******/ #include "gen_types_menu.h" printf("Type in an integer for the generator type: "); scanf("%d", >ype); /* read args & initialize ********************************************* */ initialize_function(&n, &in_old, &n_old, filename); in = count_in_circle(n); /* count samples in circle */ in += in_old; /* # in circle, in all runs */ n += n_old; /* # of samples, in all runs */ pi = (4.0*in)/n; /* estimate pi */ error = fabs(pi - EXACT_PI); /* determine error */ stderror = 4*sqrt(p*(1.0-p)/n); /* standard error */ printf( "pi is estimated as %18.16f from %12d samples.\n", pi, n); printf( "\tError = %10.8g, standard error = %10.8g\n", error, stderror); save_state(filename, in, n); /* check-point final state */ } int count_in_circle(n) /* count # of samples in circle */ int n; { int i, in; double x, y; for (i=in=0; i