Skip to content

Instantly share code, notes, and snippets.

@SirSaleh
Last active September 18, 2015 06:52
Show Gist options
  • Save SirSaleh/11944d5bd45931abb99a to your computer and use it in GitHub Desktop.
Save SirSaleh/11944d5bd45931abb99a to your computer and use it in GitHub Desktop.
monte carlo pi estimate for weblog
MCEstimator <- function(n = 10000){
#circ counts number of samples which are in the circle!
circ=0
for (i in c(1:n)){
x=runif(1,-1,1)
y=runif(1,-1,1)
if ((x^2+y^2)<=1){
circ=circ+1
}
}
pai=(circ/n)*4
return (pai)
}
##using function
n = 80000;
pai = MCEstimator(n)
print (paste("Monte Carlo estimate of PI with",n,"samples is:",pai),quote=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment