【问题标题】:Simulating a dice game in R; doesn't seem random在 R 中模拟骰子游戏;似乎不是随机的
【发布时间】:2021-08-05 01:10:23
【问题描述】:

我正在尝试使用以下标准模拟骰子游戏: (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 时才停止游戏。

目前,当我调用该函数时,会打印“A 6 was rolling”或“You have rolled 7 times”。我不知道如何使函数最多滚动 6 次,但如果 roll == 6 则停止。

【问题讨论】:

  • 您在寻找更优化的策略吗?我认为如果你投到第 5 卷,如果你投到 4/5/6,你应该停止,因为其中任何一个都超过你从第 6 卷得到的 3.5 EV。同样,在第 4 卷上,你应该以 5 或 6 停止,因为掷 5+6 的 EV 是 4.25。 (b/c 你有 50% 的机会以 4/5/6 [EV 5] 结束,或者有 50% 的机会以 EV 3.5 继续掷出 6。等等。

标签: r function dice


【解决方案1】:

答案的第一部分,你有两个答案,因为: 运行骰子 6 次时,1-6 经常发生。 2- 当 n == 7 时,while 循环将停止,因此您将始终有 7 次。

要解决第二种情况,您可以打印 n-1 或将 n 初始化为 0 并且 n

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()

【讨论】:

    【解决方案2】:
    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"
    

    【讨论】:

      【解决方案3】:

      这个问题只是nerd-sniped我,所以这里有一个循环,可以为您提供每次滚动的最佳策略。如果您在第 2 次或之后的结果中得到 5,或者在第 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"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-09
        • 2016-05-07
        • 1970-01-01
        • 2011-01-19
        • 2011-07-18
        • 2014-03-16
        • 2016-02-07
        相关资源
        最近更新 更多