一个比较啰嗦的π的模拟。运用投飞镖法计算圆周率。貌似程序可以写的更简洁些。 require(plotrix)
require(grid)
simp <- function(n){
x <- runif(n)
y <- runif(n)
xy <- x^2 + y^2
ind <- xy <=1
ind2 <- ind+1
up <- length(xy[which(xy <= 1)])
pai <- up /n * 4
plot(c(0, 1), c(0, 1), type = 'n', asp = 1,
main = c('pi = ', paste(pai)))
rect(0, 1, 1, 0)
draw.circle(0, 0, 1)
points(x, y, pch = 20, cex = .5,
col = c('red', 'blue')[ind2])
}
simp(10)
simp(100)
simp(500)
simp(1000)
simp(2000)
simp(10000)




雷达卡








京公网安备 11010802022788号







