#Generates n observations from a normal distribution by using the acceptance #method with a half-normal and exponential distribution. Outputs the sample # and two graphs. A normal QQ plot is produced by default. halfnorm=function(n,mean=0,sigma=1,pl=T){ x1=NULL while(length(x1)<=n){ u=runif(1) v=runif(1) x=-log(u) ex=exp(-(x^2+1)/2) x1=c(x1,x[(v*u<=ex)]*(2*(u*v>=ex/2)-1)) } x2=mean+sigma*x1 if (pl==T){ win.graph() qqnorm(x2) qqline(x2) } x2 }