[R] Offtopic, HT vs. HH in coin flips

Erik Iverson eiverson at NMDP.ORG
Mon Aug 31 21:16:33 CEST 2009

Dear R-help, 

Could someone please try to explain this paradox to me? What is more likely to show up first in a string of coin tosses, "Heads then Tails", or "Heads then Heads"?  

##generate 2500 strings of random coin flips
ht <- replicate(2500,
                paste(sample(c("H", "T"), 100, replace = TRUE),
                      collapse = ""))

## find first occurrence of HT
mean(regexpr("HT", ht))+1    #mean of HT position, 4

## find first occurrence of HH
mean(regexpr("HH", ht))+1    #mean of HH position, 6

FYI, this is not homework, I have not been in school in years.  I saw a similar problem posed in a blog post on the Revolutions R blog, and although I believe the answer, I'm having a hard time figuring out why this should be? 

Erik Iverson

More information about the R-help mailing list