计算获得所有正面或所有反面次数的代码仅返回获得正面的次数

Code to count times to get all heads or all tails is returning only times to get heads

我很高兴地说我想我遇到了第一个抛硬币问题 运行ning。我正在计算在 7 次翻转试验中获得所有正面或所有反面需要多少次。理论上应该需要大约 64 次才能得到所有正面或所有反面。

coin.counter = 0                         ## initialize global counting variable
heads.tails = 0                          ## initialize global variable
while(heads.tails != 7|0){               ## do while not 7 or 0
  heads.tails = rbinom(1,7,.5)           ## 1 trial, 7 flips
  if(heads.tails != 7|0)                 ### NOT equal to 0 or 7
    coin.counter = coin.counter + 1 
  else 
    break
}

不幸的是,我认为我只得到一个值,因为当我继续 运行 这个脚本时,我不断得到 coin.counter 值在 100 年代中期。

我该如何解决这个问题?

该问题的解决方案是将 Heads.tails 设置为 NOT 0。每次我将 0 添加到循环中作为决策标准时,程序立即停止,因为该变量当时已经为 0。我不是聪明人但我努力

发布解决方案,希望它能帮助其他人。

coin.counter = 0
### This was globally set to 0 and consistently shorting my program
heads.tails = 10  ## <- Set to dummy value so it quits messing with me

#####Loops##### 
while(heads.tails != 7 & heads.tails != 0){    ## Courtesy 2revans and MrFlick 
  heads.tails = rbinom(1,7,.5)                 ## 1 trial, 7 flips
  print(heads.tails)
  if(heads.tails != 7 & heads.tails != 0){
     coin.counter = coin.counter + 1           ### NOT equal to 0 or 7
  }       
  else {
   break 
  }
}