递归检查骰子组合

问题描述 投票:0回答:1

我有一个 6 面骰子的卷。我编写了以下脚本,用于计算两个连续 6 出现的位置:

set.seed(123)

sides <- 1:6
rolls <- sample(sides, size = 10000, replace = TRUE)
counts <- c()
position <- 1

while (position < length(rolls)) {
    # check if the current and next roll are both 6
    if (rolls[position] == 6 & rolls[position + 1] == 6) {
        # If yes, add position to counts
        counts <- c(counts, position)
        #move to the next position after the pair of 6's
        position <- position + 2
    } else {
        # If no, move to the next position
        position <- position + 1
    }
}


print(counts)

 [1]   10   63   66   88  110  121  145  170  217  304  364  422  443  461  464  490  700  749  823  894  941  970 1010 1034 1038 1062 1079 1120 1147 1259 1304 1314 1359 1363 1429
 [36] 1444 1454 1462 1496 1517 1529 1566 1766 1940 2010 2060 2107 2163 2333 2377 2388 2416 2438 2469 2483 2558 2585 2603 2642 2712 2731 2738 2777 2819 2821 2877 2900 3000 3013 3087
 [71] 3180 3326 3542 3657 3695 3727 3743 3813 3816 3818 3835 3839 3875 3892 3920 4018 4021 4132 4134 4150 4247 4348 4542 4592 4609 4629 4638 4722 4756 4770 4833 4849 4859 4875 4911
[106] 4966 5083 5151 5158 5172 5207 5236 5280 5293 5360 5383 5386 5495 5502 5508 5510 5512 5570 5686 5817 5874 5891 5894 5905 5962 5969 6018 6037 6065 6224 6239 6399 6448 6488 6557
[141] 6586 6643 6666 6771 6801 6884 6975 6994 6996 7132 7171 7268 7315 7362 7379 7500 7522 7540 7571 7575 7619 7695 7713 7724 7727 7756 7829 7981 8040 8054 8093 8297 8332 8357 8368
[176] 8424 8435 8467 8527 8607 8661 8681 8705 8723 8753 8765 8772 8812 8825 8828 8839 9022 9086 9119 9168 9187 9364 9415 9422 9440 9474 9502 9514 9542 9552 9586 9659 9685 9692 9700
[211] 9743 9767 9788 9790 9845 9876 9881 9920 9933 9956

基于此 R 代码,我正在尝试完成以下任务:

  • 第 1 步:查看第一个 (6,6) 发生的位置。将此位置称为 i
  • 第 2 步:向前浏览 j=2 个位置,看看 (6,6) 是否出现在 (i+j-1, i+j-2) 处。记录是或否
  • 第4步:向前浏览j=3个位置,看看(6,6)是否出现在(i+j-1, i+j-2)处。 。记录是或否
  • 步骤5:重复步骤2-步骤4,直到到达下一次出现(6,6)的位置。
  • 第 6 步:重复第 2 步至第 5 步,直到分析了所有 (6,6) 个出现的情况

我尝试编写以下代码来执行此任务,但发现循环结构非常复杂:

sides <- 1:6
rolls <- sample(sides, size = 100, replace = TRUE)
counts <- c()
position <- 1

while (position < length(rolls)) {
    # check if the current and next roll are both 6
    if (rolls[position] == 6 & rolls[position + 1] == 6) {
        
        counts <- c(counts, position)
        #move to the next position 
        position <- position + 2
    } else {
       
        position <- position + 1
    }
}

# result data frame
result <- data.frame(current_occurrence = integer(), absolute_position = integer(), positions_away_from_current_occurence = integer(), six_six = character())

# Loop over all occurrences of (6,6)
for (i in 1:(length(counts)-1)) {
    # Get the current occurrence
    current_occurrence <- counts[i]
    
    # Initialize j
    j <- 2
    
    # Loop until we reach the next occurrence of (6,6) or the end of the rolls
    while ((current_occurrence + j <= counts[i+1])) {
        # Check if (6,6) occurred at (current_occurrence + j - 1, current_occurrence + j)
        six_six <- if (rolls[current_occurrence + j - 1] == 6 & rolls[current_occurrence + j] == 6) "yes" else "no"
        
        # add the result to the data frame
        result <- rbind(result, data.frame(current_occurrence = i, absolute_position = current_occurrence, positions_away_from_current_occurence = j, six_six = six_six))
        
    
        j <- j + 1
    }
}


print(result)

掷骰子看起来像这样:

> rolls
  [1] 3 6 3 2 2 6 3 5 4 6 6 1 2 3 5 3 3 1 4 1 1 5 3 2 2 1 6 3 4 6 1 3 5 4 2 5 1 1 2 3 4 5 5 3 6 1 2 5 5 4 5 2 1 1 3 1 6 5 1 2 4 4 6 6 3 6 6 1 6 2 1 2 4 5 5 6 3 1 4 6 1 6 1 3 6 4 1 6
 [89] 6 3 6 5 3 6 2 5 5 3 2 2

结果本身如下所示:

> head(result)
   current_occurrence absolute_position positions_away_from_current_occurence six_six
1                   1                10                                     2      no
2                   1                10                                     3      no
3                   1                10                                     4      no
4                   1                10                                     5      no
5                   1                10                                     6      no

有人可以帮助我理解我是否正确地做到了这一点吗?

谢谢!

r
1个回答
0
投票

您不需要任何循环来完成此任务。

这是一个关于如何识别双 6 的所有起始位置的简单版本。

sample_size <- 100L

set.seed(1)
rolls <- sample(1:6, sample_size, replace = TRUE)

idx <- which(rolls[seq_len(sample_size - 1L)] == 6L & rolls[-1L] == 6L)
idx
15 52 57 58

这意味着 6-6 出现了 4 次。 位置 15-16、52-53、57-58 和 58-59

© www.soinside.com 2019 - 2024. All rights reserved.