-
Notifications
You must be signed in to change notification settings - Fork 0
/
MontyHall.r
38 lines (36 loc) · 820 Bytes
/
MontyHall.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
26
27
28
29
30
31
32
33
34
35
36
37
38
simulate.round <- function(n.doors, policy){
ChosenDoor = sample(1:n.doors,1)
rightDoor = sample(1:n.doors,1)
if (policy == "KEEP"){
if (ChosenDoor == rightDoor) {
return (1);
}
else{
return (0);
}
}
if (policy == "CHANGE"){
if (ChosenDoor != rightDoor) {
return (1);
}
else{
return (0);
}
}
}
main <- function(policy, n.min, n.max, n.trials){
win.probability <- rep(0, n.max)
for(n.doors in n.min:n.max){
win <- 0
for(i in 1:n.trials){
win <- win + if(simulate.round(n.doors, policy)){
1
} else {
0
}
}
win.probability[n.doors] <- win/n.trials
}
plot(3:n.max, win.probability[3:n.max], type="l", xlab="# of Doors", ylab="Win Probability", main=policy)
}
main("NAME", 3, 100, 10*1000)