Skip to content

Instantly share code, notes, and snippets.

@lbozhilova
Last active February 2, 2016 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbozhilova/95e48edfda208447c5b7 to your computer and use it in GitHub Desktop.
Save lbozhilova/95e48edfda208447c5b7 to your computer and use it in GitHub Desktop.
R code for simple Monte Carlo estimation of pi.
require (plotrix)
require (grid)
plot(c(-1, 1), c(-1,1), xlab = "x", ylab="y", type = "n", asp=1)
rect( -1, -1, 1, 1)
draw.circle( 0, 0, 1 )
N <- 150
x <- runif(N, min=-1, max=1)
y <- runif(N, min=-1, max=1)
points (x, y, col="red", pch=19)
PiEst = 0
for (i in 1:N){
if ((x[i]^2+y[i]^2)<1)
PiEst = PiEst+1
}
PiEst = 4*PiEst/N
PiEst
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment