The Monty Hall problem is a problem based on probability, named after Monty Hall. It became famous because of a reader’s letter to Marilyn vos Savant asking her about it:

*Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?*

Her response was that the player should switch to the other door. Her reasoning was that players who switch have two-thirds chance of winning the car, while players who stick to their choice have only one-third chance.

Let’s simulate the game and estimate the probability of winning by switching or not switching from your choice.

We will simulate the game for 10,000 iterations for each of the two strategies by using a `for()`

loop. Each iteration represents a round. So we have to randomize the vector of items for each iteration, because it has to start fresh everytime the game is played. A player picks a door at random. Then out of the remaining doors, the host opens one door with the goat. The player then gets a chance to either stick to the original selection or switch.

```
vec = c() # create an empty vector to concatenate the results of the iterations
for(i in 1:10000) {
# randomize the doors as the host of a specific game can decide what goes behind each door
doors = sample(c("goat", "car", "goat"), 3, replace = FALSE); doors
# a player picks a door at random
s = sample(1:3, 1, replace = FALSE); s
playerchoice = doors[s]; playerchoice
# find what is behind the remaining doors
remaining = doors[-s] ; remaining
# out of the remaining, the host has to open the door with the goat behind it
remaining = remaining[order(remaining)] ; remaining
remaining[2] # door opened
remaining[1] # door not-opened
if(remaining[1] == "car") {
vec = c(vec, i)
}
}
# probability of winning
pwin.switch <- length(vec)/i; pwin.switch
```

`## [1] 0.6665`

```
vec = c() # create an empty vector to concatenate the results of iterations
for(i in 1:10000) {
# randomize the doors as the host of a specific game can decide what goes behind each door
doors = sample(c("goat", "car", "goat"), 3, replace = FALSE); doors
# a player picks a door at random
s = sample(1:3, 1, replace = FALSE); s
playerchoice = doors[s]; playerchoice
if(playerchoice == "car") {
vec = c(vec, i)
}
}
# probability of winnning
pwin.noswitch <- length(vec)/i; pwin.noswitch
```

`## [1] 0.3322`

As you saw above, the probability of winning a car if you do not switch is one-third, while with switching it is two-thirds.

```
# create barplot to represent the probability
probplot = barplot(c(pwin.noswitch, pwin.switch), col = c("#CC00FFFF", "#CC00FFFF"), main = "Probabilities", horiz = TRUE, names.arg = c("no switch", "switch"))
```