//This reproduce Fig. 4.12 in DeVries // generate data for dist of Monte Carlo integration of I(exp(x)) in [0,1] // with N=100 and N=400 #include #include #include "random.h" #include "numcip.h" int seed;//seed for random number generator double integrant(double x); main() { double mcsum; int N,n,i; FILE *outfile; char filename[25]; seed=-1; printf("Please input N value for Monte Carlo\n"); scanf("%d",&N); printf("Output file name\n"); scanf("%s",filename); outfile=fopen(filename,"w"); fprintf(outfile,"mcInt\n"); for(n=1;n<=10000;n++)//this loop collect points for distribution { mcsum=0.0; for(i=1;i<=N;i++)//this loop actually calculate the avg f for Monte Carlo { mcsum+=integrant(ran1(&seed));//ran1() gives a random number in [0,1] } fprintf(outfile,"%lg\n",mcsum/N);//range of integration is simply one } return 0; } double integrant(double x) { return exp(x); }