Skip to content

Instantly share code, notes, and snippets.

@cutalion
Last active May 6, 2016 11:02
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 cutalion/768b3d179102fa8aa01c7228a807a6e1 to your computer and use it in GitHub Desktop.
Save cutalion/768b3d179102fa8aa01c7228a807a6e1 to your computer and use it in GitHub Desktop.
Monty Hall problem experiment https://en.wikipedia.org/wiki/Monty_Hall_problem
N = 10_000
matched1 = 0
matched2 = 0
N.times do
seq = [1, 0, 0].shuffle
guess = rand(3)
matched1 += 1 if seq[guess].eql?(1)
index_zero = seq.each_with_index do |item, index|
break index if (index != guess && item == 0)
end
seq.delete_at(index_zero)
guess = guess - 1 if guess > index_zero
matched2 += 1 if seq[guess].eql?(0)
end
puts matched1 / N.to_f
puts matched2 / N.to_f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment