# 掷硬币500次
N <- 500
flipsequence <-sample(x=c(0,1),size = N,replace = TRUE,prob = c(0.5,0.5))
r <- cumsum(flipsequence) ##统计每项前出现1的次数矩阵
n <- 1:N ##1~500的顺序
runprop = r/n
#绘图
plot(n,runprop,type="o",log="x",
xlim=c(1,N), ylim=c(0.0,1.0), cex.axis=1.5, ##cex.axis设计字体粗细
main="Running probability of Heads")
#添加一条直线y=0.5
lines(c(1,N),c(0.5,0.5),lty=3)
#添加标注
flipletters <- paste(c("T","H")[flipsequence[1:10]+1], collapse = "")
displaystring <- paste("FlipSequence= ", flipletters,'...',sep="" )
text(5,0.9, displaystring, adj = c(0,1),cex=1.3)
#显示模拟500次得到Head的频率
text(100,0.9, paste("End probability = ",runprop[N]),adj=c(1,0), cex=1.3)