/* normal, straightforward Monte Carlo and
	important sampling Monte Carlo (with p(x)=3/4(1-x^2))
	integration of the function f(x)=(1-x^2)*exp(-x^2) on
	the [-1,1] interval  */


#include<stdlib.h>
#include<stdio.h>
#include<math.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));
}


double Gen2(double z)
{double s, w1, w2;
 w1=x1*x1*x1-3.0*x1+4.0*z;
 for(s=x1+0.001; s<=x2; s+=0.001)
 {w2=s*s*s-3.0*s+4.0*z;
  if (w2*w1<=0) return(s);
 }
 printf("baj van\n");
 return(s);
}

main()
{long int i;
 double I1, I2, I3, 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);
 I3=0;
 for(i=1;i<=Nmax/10.0;i++)
	{x=randf()-0.5;
	 x=Gen2(x);
	 I3+=4.0/3.0*exp(-x*x);
	}
 I3=I3/(double)(Nmax)*10.0;

 printf("%lf     %lf     %lf\n", I1, I2, I3);
 getchar();
}
