Skip to content

Instantly share code, notes, and snippets.

@mvw
Last active October 22, 2017 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvw/ef45afdcaeda634d5452f4f51caa998d to your computer and use it in GitHub Desktop.
Save mvw/ef45afdcaeda634d5452f4f51caa998d to your computer and use it in GitHub Desktop.
def run
urn = [:blue, :blue, :red, :red, :red]
blue = 0
n = urn.size
(1..n).each do |k|
index = rand(urn.size)
ball = urn.delete_at(index)
#puts "ball=#{ball} urn=#{urn}"
if ball == :blue
blue += 1
end
if blue == 2
if k % 2 == 1
return :alice
else
return :david
end
end
end
throw :error
end
def experiment(n)
alice = 0
n.times do |i|
winner = run()
#puts "#{i}: #{winner}"
if winner == :alice
alice += 1
end
end
p = alice / (1.0 * n)
puts "n=#{n} alice wins=#{alice} => p=#{p}"
end
experiment(1_000_000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment