[R] Head or Tails game

darnold dwarnold45 at suddenlink.net
Sat Aug 4 06:18:14 CEST 2012


Wow! Some great responses!

I am getting some great responses. I've only read David, Michael, and Dennis
thus far, leading me to develop this result before reading further.

lead <- function(x) {
  n <- length(x)
  count <- 0
  if (x[1] >= 0) count <- count + 1
  for (i in 2:n) {
    if (x[i] > 0 || (x[i] == 0 && x[i-1] >= 0 )) {
      count  <- count + 1
    }
  }
  count
}

games <- replicate(10000,sample(c(-1,1),40,replace=TRUE))

games_sum <- apply(games,2,sum)
plot(table(games_sum))

games_lead <- apply(games,2,cumsum)
games_lead <- apply(games_lead,2,lead)
plot(table(games_lead))

Now I am going to read Arun, William, and Jeff's responses and see what
other ideas are being proposed.

Thanks everyone.

D.



--
View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-tp4639142p4639150.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list