**Ripples**, and kindly contributed to R-bloggers)

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 his blog:

**Ripples**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...