Here's my code
buffon <- function(a,l)
{
n<-0
N<-0
repeat {N<-N+1
print(N)
p<-c(n/N)
# Sample the location of the needle's centre.
x<-runif(1,min = 0,max =a/2)
print(x)
# Sample angle of needle with respect to lines.
theta<-runif(1, 0, pi/2)
print(theta)
# Does the needle cross a line?
k<-l/2*sin(theta)
ifelse(x<=k,n<-n+1,n<-n)
p<-c(n/N)
print(p)
pie<-(2*l)/(p*a)
print(pie)
if(N>5000) {break}
}
}
I am trying to estimate the value of pi using the idea of Buffon's needle, however, when I try:buffon(2,3), the final estimation is 3.8, which is far greater than 3.1. Could someone explain to me if there's any mistakes in my code or I cannot use pi to estimate pi?
Addition:
I realized that many lines of my code are redundant so I modified it a bit this morning:
buffon01 <- function(n,a,l)
{
# Sample the location of the needle's centre.
x<-runif(n,min = 0,max =a/2)
# Sample angle of needle with respect to lines.
theta<-runif(n, 0, pi/2)
# Does the needle cross a line?
k<-l/2*sin(theta)
# l is the length of the needle
# a is the distance between to parallel line
v<-length(x[x<=k])
p<-c(v/n)
pie<-(2*l)/(p*a)
list("pi"=pie,"error"=abs(pie-pi))
}
By setting a way larger than l I am able to get a fairly close result to 3.14... but the result I get is very unstable... as in if I do the same experiment again 3.1* could be any number other than 4... Did I ignore some other problems in my setting?