编写一个简单的 for 循环
让我们编写一个 for 循环来多次重复某个过程,并将结果逐步存入一个向量。我们将使用上一个练习中创建的函数来掷骰子。
本练习是课程的一部分
R 中的概率谜题
练习说明
- 在
for循环中指明索引应从 1 到 10000。 - 为
output填入索引,用于存储每次的结果。 - 为
roll_dice填入参数,以掷两个骰子。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Initialize a vector to store the output
output <- rep(NA, 10000)
# Loop for 10000 iterations
for(i in ___){
# Fill in the output vector with the result from rolling two dice
output[___] <- roll_dice(___)
}