円周率を求めるMCシミュレーション

#Pi; Ratio of the circumference of a circle to its diameter

N <- 100000

x <- y <- x1 <- y1 <- x2 <- y2 <- numeric(N)

x <- runif(N)
y <- runif(N)
pi <- sum(x^2 + y^2 <= 1) * 4 / N
print(pi)

for (i in 1:N) {
  if(x[i]^2 + y[i]^2 <= 1){
  x1[i] <- x[i]
  y1[i] <- y[i]
 }else{
  x2[i] <- x[i]
  y2[i] <- y[i]
 }
}

par(pty="s")
plot(x1, y1, col="red", xlab = "X", ylab = "Y", cex=0.2)