
/* normal and Monte Carlo integration of the
	(1-x^2)*exp(-x^2) function on the [-1,1]
	interval                                  */


#include<stdlib.h>
#include<stdio.h>

#define  Nmax 100000
#define x1  -1.0
#define x2   1.0

double randf()
{return( (double)(rand())/(double)(RAND_MAX+1.0));
}

double function( double z)
{ return( (1-z*z)*exp(-z*z));
}


main()
{long int i;
 double I1, I2, dx, x;
 I1=0;
 I2=0;
 dx=(x2-x1)/(double)(Nmax);
 x=x1;
 for(i=1; i<=Nmax; i++)
	{I1+=function(x)*dx;
	 x+=dx;
	}
 for(i=1; i<=Nmax; i++)
	{x=randf()*(x2-x1)+x1;
	 I2+=function(x);
	}
 I2=(x2-x1)*I2/(double)(Nmax);
 printf("%lf     %lf\n", I1, I2);
 getchar();
}
