## Plot the winnings of a simple H&T game ## where you win or lose 1 penny each time n <- 1000 ev <- sample(c(1,-1), n, replace=T) winnings <- cumsum(ev); plot(winnings, type='l') ## Count the number of games during which ## the winnings exceed 5 pennies ngames <- 10 goalAchieved <- vector(length=ngames) for(k in 1:ngames) { winnings <- cumsum(sample(c(1,-1), n, replace=T)) goalAchieved[k] <- any(winnings>=5) } timesGoalAchieved <- length(which(goalAchieved)) probGoalAchieved <- timesGoalAchieved / ngames