Created
August 3, 2016 16:52
-
-
Save mattsedlar/e55fef9519c3a25e72114b8e0bc3ba42 to your computer and use it in GitHub Desktop.
Monty Hall Problem simulation script in R
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
doors = c(1:3) | |
tossout = function (x,y,z) { | |
if(x == y) { | |
return(sample(z[which(z!=x)],1)) | |
} else { return(z[which(z!=x & z!=y)]) } | |
} | |
results = function(x,y) { | |
if(x!=y) { return(0) } | |
else { return(1) } | |
} | |
game = function(d) { | |
# check if user picks a correct number | |
if(d < 1 | d > 3) { | |
stop("Pick a door: 1,2,3") | |
} | |
# user's door | |
print(d) | |
# now game selects a door | |
game.door = sample(doors,1) | |
print(game.door) | |
# this is where the game picks the door to remove | |
empty.door = tossout(d,game.door,doors) | |
# game removes remaining door | |
print(empty.door) | |
results(d,game.door) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment