# Do Not Play With Mr. Penney

**Ripples**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Facts do not speak (Henry Poincare)

Mr. Penney is my best friend. He is maths teacher and loves playing. Yesterday we were in his office at the university when he suggested me a game:

*When you toss a coin three times, you can obtain eight different sequences of tails and heads: TTT, TTH, THT, HTT, THH, HTH, HHT and HHH. Using a fair coin, all sequences have the same chances to appear. Choose one sequence and I will then choose another one. I will toss a coin until either your or my sequence appears as a consecutive subsequence of the coin toss outcomes. The player whose sequence appears first wins. I will repeat this procedure 100 times. The one with more games won is the winner of the game. Don’t worry: I will not toss the coin manually. I will simulate using my computer. What’s your bet?*

*Ok, my bet is THT*, I said. After some seconds, Mr. Penney said: *My bet is TTH.*

This was the result of the first round:

*Another chance?* told me Mr. Penney. *Of course! Now my bet is TTH!* I said. In fact, I was thinking *Take that! Now I chose your previous bet. Do you think I am foolish?*. After some seconds, Mr. Penney said: *My bet now is HTT*.

This was the result of the second round:

*Another chance?* told me Mr. Penney. At this point, I was very suspicious but I wanted the last chance so I told him *Of course! Now my bet is HTT! *I wanted to try my strategy one more time. After some seconds, Mr. Penney said: *My bet now is HHT*.

This was the result of the third round:

*Ok, I give it up! What’s the trick?* I said. And Mr. Penney explained it to me. You can find the explanation here. *This is the last time I play with you!* I told him once he finished the explanation.

Here you have the code. Feel free to play:

library(gridExtra) Me <- "TTH" Penney <- "HTT" results <- data.frame(play= numeric(0), Penney = integer(0), Me = character(0)) for (i in 1:100) { play <- c() repeat {play <- do.call(paste, c(play, as.list(sample(c("H","T"), 1)), sep="")) if (grepl(Penney, play)|grepl(Me, play)) { results <- rbind(results, data.frame(play= i, Penney = as.numeric(grepl(Penney, play)), Me = as.numeric(grepl(Me, play)))) break}}} grid.newpage() table <- rbind( c("Me", Me, sum(results$Me), if(sum(results$Penney) > sum(results$Me)) "Loser" else "Winner"), c("Penney", Penney, sum(results$Penney), if(sum(results$Penney) > sum(results$Me)) "Winner" else "Loser")) grid.table(table, cols = c("Player", "Bet", "Games Won", "Result"), gpar.colfill = gpar(fill="palegreen3",col="White"), gpar.corefill = gpar(fill="palegreen",col="White"), gpar.rowfill = gpar(fill=NA, col=NA))

**leave a comment**for the author, please follow the link and comment on their blog:

**Ripples**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.