Last active
September 18, 2015 06:52
-
-
Save SirSaleh/11944d5bd45931abb99a to your computer and use it in GitHub Desktop.
monte carlo pi estimate for weblog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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