Created
July 18, 2016 11:03
-
-
Save harsh183/5168b41e4cfb22efee29f271d87c158c to your computer and use it in GitHub Desktop.
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
# Basic implementation of the monty hall problem based on | |
# http://mathforum.org/dr.math/faq/faq.monty.hall.html | |
# | |
# Harsh Deep | |
# 18.7.2016 | |
# This is a acceptable door configuration, :goat means that there is a goat | |
# behind and :car means that there is a car behind. | |
doors = [:goat, :car, :goat] | |
# These two variables hold the count of which choice would count as a win | |
stay_wins, change_wins = 0, 0 | |
# The number of trials is 10000 per the problem, but the higher number, the | |
# greater confidence in the results | |
number_of_trials = 10000 | |
# Here is the loop that runs the evaluation based on the above number | |
number_of_trials.times do | |
# Contestant picks a door | |
door_picked = rand(3) | |
# If the current door is a car it's stay wins, if not it's change wins | |
if doors[door_picked] == :car | |
stay_wins += 1 | |
else | |
change_wins += 1 | |
end | |
end | |
# Calculate probability | |
# The function to_f converts the win counts to floats for percentage calculation | |
stay_probability = stay_wins.to_f/number_of_trials * 100 | |
change_probability = change_wins.to_f/number_of_trials * 100 | |
# This part is just for the output. | |
# Note: puts prints string on screen, a puts command without anything after | |
# prints an empty line | |
puts "For #{number_of_trials} trails, the results are:" | |
puts | |
puts "Stay wins: #{stay_wins}" | |
puts "Change wins: #{change_wins}" | |
puts | |
puts "Stay win probabilty: #{stay_probability}%" | |
puts "Change win probabilty: #{change_probability}%" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment