Skip to content

Instantly share code, notes, and snippets.

@sedaghatfar
Created March 23, 2013 23:05
Show Gist options
  • Save sedaghatfar/5229672 to your computer and use it in GitHub Desktop.
Save sedaghatfar/5229672 to your computer and use it in GitHub Desktop.
[R] Monte Carlo Technique
# For this exercise we assume to draw random points inside the square on the [-1,1] unit,
# and thus the value of Pi = 4(# random pts insid cirlce / # random pts in square)
sim.pi <- function(iterations = 1000) {
# Generate two vectors for random points in unit circle
x.pos <- runif(iterations, min=-1, max=1)
y.pos <- runif(iterations, min=-1, max=1)
# Test if draws are inside the unit circle
draw.pos <- ifelse(x.pos^2 + y.pos^2 <= 1, TRUE, FALSE)
draws.in <- length(which(draw.pos == TRUE))
# Estimate Pi
return(4*(draws.in/iterations))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment