【问题标题】:Changing values of a dice roll更改掷骰子的值
【发布时间】:2021-06-18 07:46:27
【问题描述】:

假设我掷了 5 个骰子。

模拟滚动的代码是

Rolls<-sample(1:6, 5, replace=TRUE)

如果我想将我的卷存储在对象 Rolls 下。

现在假设出于某种原因我不希望有超过 2 个六。这意味着,例如,如果我滚动 6 3 5 6 6 1 ,我是否能够将 6 个值之一重新滚动为一个新值,以便只有 2 个值 6 和 4 个值不是 6?

我们将不胜感激。

提前致谢

【问题讨论】:

  • 另外,我还有一个问题,不妨在这里问一下,如何从样本中删除值?那么,假设我想要求用户删除一个卷,然后从样本中删除他们说的值,我该怎么做?
  • 第二个问题取决于用户如何指定要删除哪个卷,他是要说卷在向量上的位置(“删除第三行”),还是别的什么?
  • 假设我们在我的第一个问题中使用卷,我们问用户“您要删除哪个卷?”假设他们选择 3。然后我将如何删除该 3 值或他们选择的任何值?
  • 因此,用户将针对特定值而不是该值的位置,但是,假设他们选择 3,我可以使用根据其位置删除该值的代码。我的意思是,如果他们选择 3,例如,删除样本中的第二个值(即 3 所在的位置)的代码对我来说也可以
  • 好的,唯一的问题是你有replace=TRUE,你可能有不止一个 3 值,在这种情况下你想删除所有的 3 吗?

标签: r sample dice


【解决方案1】:

没有循环的解决方案可能是:

condition = which(Rolls==6)
if(length(condition)>=3){
  Rolls[condition[3:length(condition)]] = sample(1:5, length(condition)-2, replace=TRUE)
}

condition 以 6 表示 Rolls 中的位置,如果超过 2 个,则从Rolls[condition[3:length(condition)]] 开始选择第三个并重新采样。

第二个问题可能是这样的:

remove = 3
Rolls = Rolls[-which(Rolls==remove)[1]]

如果您愿意,可以轻松地将它们放入函数中

编辑 1

要使第二个答案更具交互性,您可以为其构建一个函数:

remove.roll = function(remove, rolls){
   rolls = rolls[-which(rolls==remove)[1]]}

然后用户可以使用他喜欢的任何remove 调用该函数。您还可以制作一个从提示中获取信息的程序:

remove = readline(prompt="Enter number to remove: ")
print(Rolls = Rolls[-which(Rolls==remove)[1]])

【讨论】:

  • 只是一个简单的问题,您对我的第二个问题的回答完美,但是,我是否也可以要求用户删除一个值,将其存储为一个对象(我们称之为“Player_Remove”)和然后设置“remove = Player_Remove”,这是可能的还是必须将“remove”设置为数值?
  • 好的,所以在我的代码中我使用了提示符,我设置了“first_number
  • 所以我有类似 print(Rolls = Rolls[-which(Rolls==first_number)[1]]) 的东西?
  • 我想是的,我需要更多关于整个程序的上下文来确定,如果你愿意,我们可以在聊天室继续这个问题:chat.stackoverflow.com/rooms/230192/…
【解决方案2】:

这是我的这个函数的版本,它使用递归来滚动额外的值,所以我们只有不超过 2 个6s。请注意,我将rolls 向量放在函数外部,因此为了从函数内部替换第三、第四或...6,我们使用复杂的赋值运算符<<-。 我个人选择在运行 3 个 6s 或更多时修改第一个 6 值。

rolls <- sample(1:6, 6, replace = TRUE)

n_six <- function() {
  n <- length(rolls[rolls == 6])
  
  if(n <= 2) {
    return(rolls)
  } else {
    extra <- sample(1:6, 1, replace = TRUE)
    rolls[which(rolls == 6)][1] <<- extra
  }
  n_six()
}

# Imagine our rolls would be a vector with 3 six values like this

rolls <- c(1, 2, 6, 5, 6, 6)

> n_six()
[1] 1 2 3 5 6 6       # First 6 was replaced

# Or our rolls contains 4 six values

rolls <- c(1, 6, 6, 5, 6, 6)

> n_six()
[1] 1 4 1 5 6 6       # First 2 6s have been replaced

等等……

【讨论】:

    【解决方案3】:

    我们可以将其作为scan() 用例的有趣演示。您可以输入要替换的值的位置。请注意,您需要逐个提交scan()每个位置值并在每个位置后按回车,最后您可以通过交出一个空字符串""并按回车来结束输入。

    代码

    dice.roll <- function(){
      # Initial toss
      Rolls <- sample(seq(1, 6), 5, replace=TRUE)
      
      # Communicate
      cat("The outcome of the dice roll was:", "\n\n", Rolls, "\n\n", 
          "would you like to reroll any of those values ?", "\n",
          "If yes enter the positions of the values you would \n like to replace, else just input an empty string.")
      
      # Take input 
      tmp1 <- scan(what = "")
      
      # Replace
      Rolls[as.numeric(tmp1)] <- sample(seq(1, 6), length(tmp1), replace=TRUE)
      
      # Return
      cat("You succesfully replaced", length(tmp1), "elements. Your rolls now look as follows: \n\n", 
          Rolls)
    }
    
    dice.roll()
    
    # The outcome of the dice Roll was: 
    #
    #  6 4 6 3 4 
    #
    #  would you like to reroll any of those values ? 
    #  If yes enter the positions of the values you would 
    #  like to replace, else just input an empty string.
    # 1: 1
    # 2: 3
    # 3: ""
    # Read 2 items
    # You succesfully replaced 2 elements. Your set now looks as follows 
    #
    #  2 4 2 3 4
    

    请注意,此函数只是一个快速编写以正确实现它,您应该使用while 语句或recursion 重复替换,只要您愿意。此外,在实际使用之前,必须插入 if 语句来处理太长的输入和其他可能导致错误的用户行为。

    【讨论】:

      【解决方案4】:

      如果我理解正确,那应该可以:

      n <- 10
      (Rolls<-sample(1:6, n, replace=TRUE))
      #>  [1] 6 2 4 1 1 6 5 2 1 6
      (Nr_of_six <- sum(6 == Rolls))
      #> [1] 3
      
      while (Nr_of_six > 1) {
          extra_roll <- sample(1:6, 1, replace=TRUE)
          second_six <- which(Rolls==6)[2]
          Rolls[second_six] <- extra_roll
          print(Rolls)
      
          Nr_of_six <- sum(6 == Rolls)
      }
      #>  [1] 6 2 4 1 1 4 5 2 1 6
      #>  [1] 6 2 4 1 1 4 5 2 1 3
      print(Rolls)
      #>  [1] 6 2 4 1 1 4 5 2 1 3
      

      reprex package (v1.0.0) 于 2021-03-21 创建

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        • 1970-01-01
        • 2012-02-29
        • 2015-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多