/***************************************************************************/ /* ____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 #include #define SIMPLE_SPRNG /* simple interface */ #include "sprng_cpp.h" using namespace std; #define EXACT_PI 3.141592653589793238462643 int gtype; /*--- */ int initialize_function(int * n, int * in_old, int * n_old, char * filename); int count_in_circle(int n); int save_state(char * filename, int in, int n); int main(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 */ #include "gen_types_menu.h" printf("Type in a generator type (integers: 0,1,2,3,4,5): "); scanf("%d", >ype); initialize_function(&n, &in_old, &n_old, filename); /* read args & initialize */ 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 */ return 0; } int count_in_circle(int n) /* count # of samples in circle */ { int i, in; double x, y; for (i=in=0; i