secret
Created

Monty Monte Sim Loop

  • Download Gist
montyMontySimloop.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
########## 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 #########

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.