-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMontyHallBetter.R
78 lines (60 loc) · 2.64 KB
/
MontyHallBetter.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
doors <- c("1","2","3")
set_size <- 5 #The number of time this model runs the trials
trials <- c(1, 10, 100, 500, 1000) #Vector with predefined values for number of trials
trials_index <- 1 #Used to indicate position for trials vector
#series of objects for storing results for each size of trials
trial_results_1 <- c()
trial_results_10 <- c()
trial_results_100 <- c()
trial_results_500 <- c()
trial_results_1000 <- c()
for (i in 1:set_size){
while(trials_index <= length(trials)){
for(k in 1:trials[trials_index]){
#assigns the winning door
win <-sample(doors)[1]
#contestant chooses a random door
contestant <- sample(doors)[1]
#contestant is "shown" a door which is not the winning door...
#...and not the door chosen by the conestant...
#...This door is removed as an option if/when the contestant chooses
#...to change doors.
show <- sample(doors[which(doors != win & doors != contestant)])[1]
#Contestant changes doors
change <- doors[which(doors !=contestant & doors != show)]
#sends the results to appropriate data objects
if(contestant==win){
switch(trials_index,
{trial_results_1 <- c(trial_results_1, "staywin")},
{trial_results_10 <- c(trial_results_10, "staywin")},
{trial_results_100 <- c(trial_results_100, "staywin")},
{trial_results_500 <- c(trial_results_500, "staywin")},
{trial_results_1000 <- c(trial_results_1000, "staywin")})
}
if(change==win){
switch(trials_index,
{trial_results_1 <- c(trial_results_1, "changewin")},
{trial_results_10 <- c(trial_results_10, "changewin")},
{trial_results_100 <- c(trial_results_100, "changewin")},
{trial_results_500 <- c(trial_results_500, "changewin")},
{trial_results_1000 <- c(trial_results_1000, "changewin")})
}
}
trials_index <- trials_index + 1
}
trials_index <- 1
#when the loop finishes, adds one to the trials_index variable
#to move to the next number of trials
cat("\n Running set number ", i)
}
length(which(trial_results_1 == "changewin"))
length(which(trial_results_1 == "staywin"))
length(which(trial_results_10 == "changewin"))
length(which(trial_results_10 == "staywin"))
length(which(trial_results_100 == "changewin"))
length(which(trial_results_100 == "staywin"))
length(which(trial_results_500 == "changewin"))
length(which(trial_results_500 == "staywin"))
length(which(trial_results_1000 == "changewin"))
length(which(trial_results_1000 == "staywin"))
----------------------------------------------------------------------