Skip to content

Instantly share code, notes, and snippets.

@btelles
Last active December 17, 2015 06:29
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 btelles/5565877 to your computer and use it in GitHub Desktop.
Save btelles/5565877 to your computer and use it in GitHub Desktop.
Monty Hall Statistics simulation script.
def random_between_1_and_3
(rand * 3).ceil
end
file = File.open('results.csv', 'w')
file.puts 'car, first_choice, expose, second_choice, win, switch, switch_and_win, stay_and_win'
1_000_000.times do
car = random_between_1_and_3
first_choice = random_between_1_and_3
expose = [1,2,3].tap { |a| a.delete(car)}.shuffle.first
second_choice = [1,2,3].tap { |a| a.delete(expose)}.shuffle.first
win = second_choice == car ? 'YES' : ''
switch = second_choice != first_choice ? 'YES' : ''
switch_and_win = win == 'YES' && switch == 'YES' ? 'YES' : ''
stay_and_win = win == 'YES' && switch != 'YES' ? 'YES' : ''
file.puts [car, first_choice, expose, second_choice, win, switch, switch_and_win, stay_and_win].join(',')
end
file.close
# Results:
# switch and win: 333203
# stay and win: 166179
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment