## ================================ SETUP ================================ ## ## I play a game against an infinitely rich opponent. I have MONEY=10 ## pennies at the beginning of the game. ## ## The game consists in tossing a coin NTRIALS=100 times. If the outcome ## of an individual toss is "heads" I get one penny from the opponent. ## If the outcome is "tails", I have to pay my opponent 1 penny. ## ## What is the probability that I become broke? ## ## To get an accurate estimate, I use NREPS=10 repetitions of the game ## (you may think of a repetition as a separate game). ## ##======================================================================== ## Number of repetitions nreps <- 100 ## Number of trials, i.e. coin tosses in a single game ntrials <- 1000 ## The amount of money I start with money <- 10 s <- vector(length=nreps) for(i in 1:nreps) { s[i] <- min(cumsum(sample(c(1,-1), ntrials,replace=T))) }; print(length(which(s<= -money))/nreps)