## Problem 37 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 37: We need \$40 to get on the bus home from Vegas tomorrow, but are down to \$20. The plan is to play evens in roulette (2:1 payout, 18/38 probability of winning), but we're missing one detail. Do we bet it all one time and walk away with \$0 or \$40, or do we bet it a dollar at a time?
```#!/usr/bin/env ruby # Bold play or cautious play? We need \$40 to get on the # bus home from Vegas tomorrow, but are down to \$20. Do # we bet it all at once on evens in roulette, or do we # bet it a dollar at a time? TRIALS=10000 P_WIN = 18.0/38.0 # 18 evens on a 38-slot roulette wheel def play_bold() return rand() < P_WIN end def play_cautious() bank = 20 plays_remaining = 10000 # limit how long we can play while (bank < 40 && bank > 0 && plays_remaining > 0) if (rand() < P_WIN) bank += 1 else bank -= 1 end plays_remaining -= 1 end return bank == 40 end win_bold = 0 win_cautious = 0 TRIALS.times { win_bold += 1 if play_bold() win_cautious += 1 if play_cautious() } puts "After #{TRIALS} times, wins:" puts " bold: #{win_bold}" puts " cautious: #{win_cautious}" ```

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.