Skip to content

Instantly share code, notes, and snippets.

@darthschmoo
Created September 16, 2015 20:00
Show Gist options
  • Save darthschmoo/c47be37311625daf82b4 to your computer and use it in GitHub Desktop.
Save darthschmoo/c47be37311625daf82b4 to your computer and use it in GitHub Desktop.
# returns the two boxes ["G", "G"], ["G", "S"] (equal odds for every order)
def get_boxes
dice_roll = rand()
if dice_roll < 0.25
[["G", "G"], ["G", "S"]]
elsif dice_roll < 0.5
[["G", "G"], ["S", "G"]]
elsif dice_roll < 0.75
[["G", "S"], ["G", "G"]]
else
[["S", "G"], ["G", "G"]]
end
end
# Always picks first coin from first box.
def place_bet
boxes = get_boxes
coin0 = boxes[0].pop
if coin0 == "S"
puts "No bet will be placed."
"0"
elsif coin0 == "G"
coin1 = boxes[0].pop
if coin1 == "S"
puts "Bryce wins!"
"B" # Bryce won!
elsif coin1 == "G"
puts "William wins!"
"W"
end
end
end
def percent_format_odds( f )
sprintf( "%0.02f %", f * 100 )
end
def run_many_bets( n )
results = Hash.new(0)
n.times do
result = place_bet
results[result] += 1
end
w = results["W"]
b = results["B"]
z = results["0"]
puts "William wins: #{w} (#{ percent_format_odds( w / n.to_f )})"
puts "Bryce wins: #{b} (#{ percent_format_odds( b / n.to_f )})"
puts "Nobody wins: #{z} (#{ percent_format_odds( z / n.to_f )})"
puts "W/B ratio: #{w / b.to_f} / 1"
results
end
puts run_many_bets( 100000 ).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment