Simulating+Craps+with+R

passLineBet <- function(n.sim){

makepoint <- function ( point ) { made <- NA while ( is.na(made) ) { # roll until outcome is determined roll <- sum ( sample ( 6, 2, replace=T ) ) if ( roll == point ) { made <- TRUE } else if ( roll == 7 ) { made <- FALSE } } # end while return ( made ) } # end makepoint sim.craps <- function { wonFirstRoll = 0 lostFirstRoll = 0 wonPoint = 0 lostPoint = 0 roll <- sum ( sample ( 6, 2, replace=T ) ) if ( roll==7 || roll==11 ) { win <- T cat("won on ",roll,"\n") wonFirstRoll = wonFirstRoll + 1 } else if ( roll==2 || roll==3 || roll==12 ) { win <- F cat("lost on ",roll,"\n") lostFirstRoll = lostFirstRoll + 1 } else win <- makepoint ( roll ) if(win) { cat("won on point ",roll,"\n") wonPoint = wonPoint + 1 } else { cat("lost on point ",roll,"\n") lostPoint = lostPoint + 1 } stuff <- c(win,wonFirstRoll,lostFirstRoll,wonPoint,lostPoint) return ( stuff ) } wins <- 0 wonFirstRoll = 0 lostFirstRoll = 0 wonPoint = 0 lostPoint = 0 for ( i in 1:n.sim ) { stuff = sim.craps wonFirstRoll = wonFirstRoll + stuff[2] lostFirstRoll = lostFirstRoll + stuff[3] wonPoint = wonPoint + stuff[4] lostPoint = lostPoint + stuff[5] } cat("wonFirstRoll: ",wonFirstRoll," lostFirstRoll: ",lostFirstRoll," wonPoint: ",wonPoint," lostPoint: ",lostPoint) ( wins/n.sim ) }
 * 1) n.sim <- 100000
 * 1) wins <- wins + sim.craps