在R中模拟骰子游戏;似乎不是随机的



我正在尝试模拟一个骰子游戏,条件如下:(1( 您最多可以掷骰子6次;(2( 在游戏过程中的任何时候,在观察到掷骰子的结果后,您都可以停止游戏,并赢得该掷骰子上显示的美元金额。例如,您的卷数为5、1、3、4,而您决定停止游戏,则您赢得4美元;你的掷骰子是5、1、3、4、3、2,没有决定停止游戏,那么你赢得了2美元。

我现在的功能是

stop_on_6 <- function() {
nrolls <- 0
# set.seed(0)
n <- 1

# generate 1 random integer from uniform distribution on [1,6] with
# equal probability.
while (n <= 6){
roll <- sample(1:6, size = 1, replace = TRUE, prob = rep(1/6, 6))
if (roll == 6) {print('A 6 was rolled')
return (roll)}

n <- n + 1
}

sprintf("You've rolled ", n, " times.")
}

我的目标函数将计算你在n游戏中的预期奖金,假设你只有在获得6分时才停止游戏。

此时,当我调用函数时,会打印";轧制6";,或";你已经滚了7次了;。我不知道如何使函数最多滚动6次,但如果roll == 6,则停止。

答案的第一部分,您既有答案,也有答案,因为:1-6经常发生在掷骰子6次时。2-while循环将在n=7时停止,因此您将始终有7次。

要修复第二种情况,您可以打印n-1或使用初始化为0的n,而n<6.我用了下面的第一个。

stop_on_6 <- function() {
n <- 1
memory = 0
while (n <= 6 & memory != 6){

roll <- sample(1:6, size = 1, replace = TRUE, prob = rep(1/6, 6))
if (roll == 6){
print('A 6 was rolled')
}
memory = roll
n <- n + 1
}
sprintf("You played %d times and won %d", n-1, memory)
}
stop_on_6()
stop_on_6 <- function(episode) {
reward <- c()
for(i in 1:episode) { 

n <- 1

while (n <= 6){
roll <- sample(1:6, size = 1, replace = TRUE, prob = rep(1/6, 6))
reward[i] <- roll
n <- ifelse(roll == 6,7,n+1)                

}
}
return(paste0("You played ", episode," episode.Your expected reward is ",mean(reward)))
}
stop_on_6(1000)

给予,

"You played 1000 episode.Your expected reward is 4.944"

这个问题让我很头疼,所以这里有一个循环,为您提供每次滚动的最佳策略。如果你在第二卷或更晚的时候得到5分,或者在第五卷得到4分,你应该退出,因为你留在里面可能会做得更糟。

dice <- 1:6
breakeven = 0  # no value of rolls after the sixth one
for(i in 6:2) {
next_roll_EV <- breakeven
values_over_future_EV = dice[dice > next_roll_EV]  # stop if you get one of these
settle_chance = length(values_over_future_EV)/6
settle_EV = mean(values_over_future_EV)
keep_going_chance = 1 - settle_chance
breakeven = settle_chance*settle_EV + keep_going_chance*next_roll_EV
stop_rolls = dice[dice > breakeven]
print(paste0("roll ", i, " has EV of ", breakeven,
", so stop in the prior roll if you have any of ", paste(stop_rolls, collapse = ", ")))
}
[1] "roll 6 has EV of 3.5, so stop in the prior roll if you have any of 4, 5, 6"
[1] "roll 5 has EV of 4.25, so stop in the prior roll if you have any of 5, 6"
[1] "roll 4 has EV of 4.66666666666667, so stop in the prior roll if you have any of 5, 6"
[1] "roll 3 has EV of 4.94444444444444, so stop in the prior roll if you have any of 5, 6"
[1] "roll 2 has EV of 5.12962962962963, so stop in the prior roll if you have any of 6"

最新更新