Create a gist now

Instantly share code, notes, and snippets.

@lbozhilova /MCpi
Last active Feb 2, 2016

What would you like to do?
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