`Doors`

is noting else than a vector representing the three doors in the Monty Hall problem. `nbSimulations`

configures the number of trials (games) we simulate.

```
doors = 1:3
nbSimulations = 1000
```

Please note we use `sample`

for randomly putting the prize behind a door and making the initial choice.

The if-else statement is for making sure the door *without* a prize is openend.

In `winWithSwitch`

and `winWithoutSwitch`

we store whether or not the final choice made resulted into a win when respectively switching and not switching doors.

`switchedChoiceDoor`

means that you do not choose your initial `choiceDoor`

and obviously you also want a different door then the one which was opened (i.e. `showDoor`

).

In `observedWinPropWithSwitch`

and `observedWinPropWithoutSwitch`

we calculate the observed winning proportion up to the current number of trials conducted.

```
for (i in 1:nbSimulations) {
prizeDoor = sample(doors, 1)
choiceDoor = sample(doors, 1)
if (choiceDoor != prizeDoor) {
showDoor = doors[doors != prizeDoor & doors != choiceDoor]
} else {
showDoor = sample(doors[doors != choiceDoor], 1)
}
switchedChoiceDoor = doors[doors != choiceDoor & doors != showDoor]
winWithSwitch[i] = switchedChoiceDoor == prizeDoor
winWithoutSwitch[i] = choiceDoor == prizeDoor
observedWinPropWithSwitch[i] = mean(winWithSwitch)
observedWinPropWithoutSwitch[i] = mean(winWithoutSwitch)
}
```

```
## Number of trials: 1000
## Number of wins when switching (proportion): 666 (0.666)
## Number of wins when not switching (proportion): 334 (0.334)
```

Plotting `observedWinPropWithSwitch`

and `observedWinPropWithoutSwitch`

allows us to see that increasing the number of trials results into a better estimation of the probabilities.