/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" c by RH Landau, MJ Paez, and CC Bordeianu c Copyright Wiley-VCH, 2007. c Electronic Materials copyright: R Landau, Oregon State Univ, 2007; c MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. c Support by National Science Foundation */ // int10d.c: Ten dimensional Monte-Carlo integration #include #include #include // if you don't have drand48 uncomment the following two lines #define drand48 1.0/RAND_MAX*rand #define srand48 srand #define max 65536 // number of trials main() { int i,j; double n = 1.0,x,y = 0; FILE *output; // save data in int_10d.dat output= fopen("int_10d.dat", "w"); for (i = 1; i<= max; i++) { // reset x x = 0; // sum of 10 x values for (j = 1; j<= 10; j++) x+= drand48(); y+= x*x; // save after 2, 4, 8, if (i%(int)(pow(2.0,n)) == 0) { n++; fprintf( output, "%i\t\t%f\n", i, y/i); } } printf("data saved in int_10d.dat\n"); fclose(output); }