Problem 9 of Monte Carlo solutions to Fifty Challenging Problems...
(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)
Problem 9: What are the odds of winning in Craps (using only Pass Line bets)?
#!/usr/bin/env ruby
# Modelling craps is weird.
# We roll two dice and sum them.
#
# if they're 7 or 11 => immediate win
# 2,3,12 => lose
# else, roll is the "point"
# player keeps rolling until:
# point => win
# 7 => lose
#
# So, what's the chance to win?
TRIALS=500000
def roll
d1 = 1 + rand(6)
d2 = 1 + rand(6)
return d1+d2
end
def win()
first_roll = roll()
return true if (first_roll == 7 || first_roll == 11)
return false if ([2,3,12].include?(first_roll))
point = first_roll
while (true) do
next_roll = roll()
return false if 7 == next_roll
return true if next_roll == point
end
end
wins = 0
TRIALS.times do
wins += 1 if win()
end
puts "After #{TRIALS} games, won #{wins}"
puts "P(win) = #{wins.to_f/TRIALS.to_f}"
I've been coding my way through Fifty Challenging Problems in Statistics with Solutions. This post is a part of the Fifty Challenging Problems series.
This was brought to you by Josh Myer. He has other fun things at his homepage.