-
-
Save robbymeals/4e5a5a7c7248ac3b65ae to your computer and use it in GitHub Desktop.
Monty Monte Sim Loop
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
########## Simulation Loop ######### | |
for (i in seq(n)){ | |
## 1. Randomly place prize behind one of three doors | |
PlacePrize <- c(1,0,0)[sample(1:3,3)] | |
## 2. Randomly pick one of three doors | |
YouPick <- Doors[sample(1:3,1)] | |
## 3. Monty either randomly opens one of the two doors left over if | |
## you happen to pick the correct door or picks the only door left | |
## if you pick one of two incorrect doors | |
MontyOpens <- ifelse(PlacePrize[Doors==YouPick]==1, | |
Doors[!Doors%in%YouPick][sample(1:2,1)], | |
Doors[(!Doors%in%c(YouPick,Doors[PlacePrize==1]))]) | |
PrizeIsBehind <- Doors[PlacePrize==1] | |
## 4. If the prize is behind the leftover door, you win if you switch. | |
## Else you win if you stick on your original choice. | |
WinIfSwitch <- ifelse(PlacePrize[!Doors%in%c(YouPick,MontyOpens)]==1,1,0) | |
Picks <- c(Picks, YouPick) | |
Opens <- c(Opens, MontyOpens) | |
WinningDoor <- c(WinningDoor, PrizeIsBehind) | |
WinsIfSwitch <- c(WinsIfSwitch, WinIfSwitch) | |
### Write results to data frames | |
PlacedDf[i,] <- PlacePrize | |
PicksDf[i,YouPick] <- 2 | |
OpensDf[i,MontyOpens] <- 3} | |
########## End Simulation Loop ######### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment