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

226 Views Asked by At

I am pretty happy to say I think I have my first coin flip problem running. I am counting how many times it takes to get all heads or all tails in a trial of 7 flips. In theory it should take around 64 times to get either all heads or all tails.

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
}

Unfortunately, I think that I am only getting one value because as I continue to run this script I keep getting coin.counter values in the mid 100s.

What can I try to fix this?

1

There are 1 best solutions below

0
CPTxShamrock On BEST ANSWER

Solution to the issue was to set the Heads.tails to NOT 0. Each time I added 0 to the loops as decision criteria the program instantly stopped because the variable was then already 0. I'm not a smart person but I try hard.

Posting the solution and hopefully it helps someone else down the line.

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 
  }
}