## Problem 7 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.)

Mr Brown has a problem: he compulsively puts a dollar on number 13 in American roulette. To help him see the error of his ways, Mr Pink gives him a bet: Pink bets Brown $20 if, after 36 rounds of roulette, Brown is ahead. (That is: if Brown is up after 36 compulsive rounds, he gets an extra $20; if he's behind, he loses an extra $20.)

#!/usr/bin/env ruby
# We'll play from Mr Brown's perspective.
#
# Modelling this game is tricky. We need to play 36
# rounds of roulette, then see if we're up. If so,
# we get an extra $20; if not, we lose $20. At the
# end, we want the expected payout per unit stake.
TRIALS=10000
def payout()
p = 0
36.times do
p += 36 if ( rand() < 1/38.to_f)
end
if p > 26
p += 20
else
p -= 20
end
# puts "This round: $36 in, $#{p} out\n"
p
end
total_payout = 0
total_bet = 0
TRIALS.times {
total_payout += payout()
total_bet += 36
}
puts "After #{TRIALS} rounds of betting, we bet #{total_bet}"
puts "We won #{total_payout}, so the expected loss is "
puts " $#{(total_bet-total_payout)/total_bet.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.