# This file requires specification of the number of trials (n), # the needle length (l) and the distance between parallel lines # (d) which should always be greater than l. buffon=function(n,l,d) { win.graph() par(mfrow =c(2,1)) Y <- runif(n,0,d/2) H <- runif(n,0,pi) x <- as.integer(Y < (l*sin(H))/2) phat <- cumsum(x)/seq(n) pihat <- (2*l)/(d*phat) ts.plot(phat,xlab="N",ylab="Estimated Proportion",ylim=c(0,1)) abline(h=(2*l)/(pi*d),col="red") title("Proportion of Crossings") ts.plot(pihat,xlab="N",ylab="Estimated Pi") title("Running Estimate of Pi") abline(h=pi,col="red") out <- list(x,pihat[n],phat[n]) names(out) <- c("Outcomes","Pihat","Phat") out }