**datum » R**, and kindly contributed to R-bloggers)

The following script simulates a craps game in R any number of iterations with any starting amount of cash and any maximum odds and running until the user is out of cash. It assumes a specific betting strategy as explained in the comments and returns a table that gives you the maximum cash generated and at what roll that happens. it returns a very simple R scatterplot of the table. it’s far from done and still returning an error randomly where you get unequal numbers of rows in the table. feel free to comment suggestions or input.

###############################################s

#craps 0.1

#takes a starting cash supply, the table minimum, the maximum odds of the table and the number of iterations desired

#and returns a table with the maximum cash and the number of the roll where the maximum was achieved

#on each iteration and a scatterplot of these values. this is all basd on a betting style wherein the

#player plays the pass line at the table minimum of $10, takes full odds, and increases the pass line bet by

#$10 each time he wins and drops back to the minimum at every loss

#

################################################

craps <- function(starting_wad, table_min, max_odds, iterations){

max_rolls <- c()

max_cash <- c()

for(j in 1:iterations){

wad <- starting_wad

field <- c(2,3,12)

wins <- c(7,11)

bet <- table_min

point <- c(4, 5, 6, 8, 9, 10)

w <- 0

i <- 1

cash <- c(wad)

rolls <- c(1)

while(wad > 0){

odds <- bet*max_odds

x <- sample(1:6,1)

y <- sample(1:6,1)

z <- x + y

#print(z)

if(z %in% field){

wad = wad – bet

#print(wad)

}

if(z %in% wins){

wad = wad + bet

#print(wad)

}

if(z %in% point){

while(w != z && w != 7){

x <- sample(1:6,1)

y <- sample(1:6,1)

w <- x + y

#print(w)

if(w == z){

if(z %in% c(4,10)){

wad = wad + bet + odds*(2)

#print(wad)

}

if(z %in% c(5,9)){

wad = wad + bet + odds*(3/2)

#print(wad)

}

if(z %in% c(6,8)){

wad = wad + bet + odds*(6/5)

#print(wad)

}

bet = bet + 10

}

if(w == 7){

wad = wad – bet – odds

bet = 10

#print(wad)

}

}

w <- 0

z <- 0

}

i <- i+1

cash <- append(cash, wad)

rolls <- append(rolls, i)

foobar <- data.frame(rolls, cash)

rolllll <- foobar$rolls[foobar$cash == max(cash)]

}

#print(“rolls”)

#print(i)

#print(“max roll”)

#print(rolllll)

#print(“maximum cash”)

#print(max(cash))

#print(foobar)

#plot(rolls, cash, type = “l”)

max_rolls <- append(max_rolls, rolllll)

max_cash <- append(max_cash, max(cash))

}

maxes <- data.frame(max_rolls, max_cash)

print(maxes)

plot(maxes$max_cash, maxes$max_rolls)

}

**leave a comment**for the author, please follow the link and comment on their blog:

**datum » R**.

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...