【发布时间】:2016-08-19 17:23:32
【问题描述】:
您好,我正在尝试解决一个玩家有 10 美元的问题。掷硬币,如果玩家正确地叫它,他赚 1 美元,如果他不正确,他输 1 美元。他在达到 20 美元之前达到 0 美元的几率是多少?游戏平均持续多长时间? 25次翻转后他平均有多少?我应该在 R 中使用蒙特卡洛方法来编写代码,但我是一个初学者,不完全确定从哪里开始——这就是我的想法
game <- function() {
x=10 ## $10
y=0 ## number of times player gets $20
z =0 ## number of times player loses money
result<- sample(1:2,1, replace = TRUE)
if (result==1) {
x=x+1 } ## money goes up, 1 represents player calling correct coin
else{
x=x-1 }
if (x= 20) {
y = y+1} ### dont know how to stop trials
if(x=0){
z=z+1}
我对如何编写代码很迷茫,但这是一个想法。基本上我想模拟一个 50/50 的模拟,看看 y 和 z 出现的频率。我不确定如何运行一定数量的试验或在达到 20 或 0 时停止....感谢您的帮助。
【问题讨论】:
标签: r simulation probability montecarlo dice