Skip to content

Instantly share code, notes, and snippets.

@Protonk
Created March 15, 2011 06:12
Show Gist options
  • Select an option

  • Save Protonk/870385 to your computer and use it in GitHub Desktop.

Select an option

Save Protonk/870385 to your computer and use it in GitHub Desktop.
Simple, but not too simple estimation of pi
pi.ratio<- function(n) {
x<- runif(n, min=-1,max=1)
y<- runif(n, min=-1,max=1)
return(4*sum(x^2 + y^2 <= 1.0)/n)
}
pi.est<- function(iter) {
sum.pi<-stor.rat<-numeric(0)
for (i in 1:iter) {
stor.rat[i]<- pi.ratio(1000)
sum.pi[i]<- sum(stor.rat[1:i])/i
}
return(sum.pi)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment