# 用R来进行布丰投针实验

March 27, 2012
By

 This post was kindly contributed by 数据科学与R语言 - go there to comment and to read the full post.

R代码如下：
rm(list=ls())
`# 绘制空白图形plot(c(0,2),c(0,2),type='n',main='布丰投针实验',xlab='X',ylab='Y')# 增加平行线abline(h=0.5)abline(h=1.5,col='red')finished <- FALSE# trial为实验次数，cross为交叉次数trial <- 0cross <- 0while (!finished) {    # Dist为针的中心距离红线的垂直距离    # Theta为针的角度    Dist <- runif(1,min=0,max=1/2)    Theta <- runif(1,0,pi)    # central.x为针中心点的横坐标    # central.y为针中心点的纵坐标    central.x <- runif(1,0.5,1.5)    central.y <- Dist +1    # 计算针两端的坐标    y1 <- sin(Theta)/4 + central.y    x1 <- cos(Theta)/4 + central.x    y2 <- sin(Theta+pi)/4 + central.y    x2 <- cos(Theta+pi)/4 + central.x    trial <- trial +1    # 计数交叉次数    cross <- cross + ifelse(0.25*sin(Theta)>=Dist,1,0)    # 绘制针的线型和中心点    lines(c(x1,x2),c(y1,y2),lty=2)    points(central.x,central.y,pch=16,col='grey')    cat('trial=',trial,'cross=',cross,'PI=',trial/cross,'\n')    #continue?    input <- readline('stop?')    # 若输入y，则结束实验    if (input =='y') finished <- TRUE}`

Tags: ,