Last active
December 17, 2015 06:29
-
-
Save btelles/5565877 to your computer and use it in GitHub Desktop.
Monty Hall Statistics simulation script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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