1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • I tried so hard to make sure I had everything for my first post too. Added R tag – CPTxShamrock Apr 13 '20 at 04:51
  • 2
    Questions asking "if this code is correct" are unfortunately considered off topic. When working with random function it's hard to check if answers are "correct." You to solve the problem mathematically and see if what your code returns seems roughly correct. But parts like `heads.tails != 7|0` don't look correct to me. You can't test for more than one value that way. You need `heads.tails != 7 & heads.tails != 0` or `!(heads.tails %in% c(0,7))` – MrFlick Apr 13 '20 at 04:59
  • ... and since that conditional is in an `if` block, don't use single `&`; while it'll work, it's misleading. `&` works with `logical` vectors (arbitrary length) while `if` blocks require length exactly 1. Also, `&` doesn't short-circuit, `&&` has the ability to short-circuit. – r2evans Apr 13 '20 at 05:17
  • 2
    BTW: while `heads.tails != 7|0` is technically legal R code, it's not what you want/think. What it's doing is `(heads.tails != 7) | (0)` which reduces to `heads.tails != 7` (since `0` is equivalent to `FALSE`). So is your function mathematically correct? Sorry, no, not until you follow @MrFlick's suggestino of using `%in%` or two comparisons (I prefer `%in%` personally). (`?Syntax` gives the order of operator precedence, and `!=` is above/before `|` and `||`.) – r2evans Apr 13 '20 at 05:21
  • This is outstanding feed back. Thank you both very much. I ran it quite a few times this morning and consistent returns over 100 gave me the feeling I was closer to 1/128 instead of 1/64. Also, to be a better patron of the forum, you mentioned this thread is off topic. Did I include the wrong tags? – CPTxShamrock Apr 13 '20 at 13:52
  • I don't know that it's [*explicitly* off-topic](https://stackoverflow.com/help/on-topic), but it's not perfectly "on", either. Typically SO is about programming problems (errors, warnings, misbehavior), but you did not frame it that way. One way to reword this to bring it back into the intended realm is *"the results are wrong: I expect 1/128 but instead I get 1/64"* (and that would have been good information to know in the question itself). – r2evans Apr 13 '20 at 14:45
  • Thank you r2evans! Title changed, target added and wishy washy talk removed. – CPTxShamrock Apr 13 '20 at 15:51

1 Answers1

0

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