experiment <- function(doors, isChangeMind=TRUE, experimentTime) {
    winTime = 0
    lostTime = 0
    i = 1
    while(i <= experimentTime) {
        prize <- sample(doors, 1)
        guess <- sample(doors, 1)
        delete <- sample(doors[! doors %in% c(prize, guess)], 1)
        reserve <- doors[doors != delete] 
        
        if (isChangeMind == TRUE) {
            guess <- sample(reserve[reserve != guess], 1)
        }
        if (guess == prize) {
            winTime = winTime + 1
        } else {
            lostTime = lostTime + 1
        }
        i = i + 1
    }
    c(experimentTime, winTime, lostTime, winTime/experimentTime)
}

resultNames <- c("doorNum", "ExpTime", 
                      "change_winTime", "change_lostTime", "changeWinRate",
                      "noChange_winTime", "noChange_lostTime", "noChangeWinRate")

expBoth <- function(doorNum = 3, experimentTime = 1000) {
    doors <- LETTERS[1:doorNum]
    
    changeResult <- experiment(doors, isChangeMind = TRUE, experimentTime)
    noChangeResult <- experiment(doors, isChangeMind = FALSE, experimentTime)
    c(doorNum, changeResult,noChangeResult[-1])
}

drawBar <- function(result) {
    names(result) <- resultNames
    subResult_names <- c('change_winTime', 'change_lostTime', 'noChange_winTime', 'noChange_lostTime')
    subResult <- result[subResult_names]
    mtx_result <- matrix(subResult, nrow = 2, dimnames = list(c("win", "lost"), c("change", "noChange")))
    print(mtx_result)
    barplot(mtx_result
            , ylab = "count", col = c("skyblue", "skyblue4")
            , main = "times of win or lost")
}

3 doors

# 1000 experiments
result <- expBoth()
drawBar(result)
##      change noChange
## win     664      363
## lost    336      637

# 3 doors, 10,000 experiments
result <- expBoth(experimentTime = 10000)
drawBar(result)
##      change noChange
## win    6697     3302
## lost   3303     6698

# 3 doors, 100,000 experiments
result <- expBoth(experimentTime = 100000)
drawBar(result)
##      change noChange
## win   66523    33403
## lost  33477    66597

10 doors

# 10 doors, 1000 experiments
result <- expBoth(10, 1000)
drawBar(result)
##      change noChange
## win     114      100
## lost    886      900

win rate from 3 to 25

# win rate from 3 to 25
df_result <- data.frame()
doorNums <- 3:25
for (doorNum in doorNums) {
     result <- expBoth(doorNum, 1000)
     df_result <- rbind(df_result, result)
}
names(df_result) <- resultNames
print(df_result)
##    doorNum ExpTime change_winTime change_lostTime changeWinRate
## 1        3    1000            680             320         0.680
## 2        4    1000            384             616         0.384
## 3        5    1000            261             739         0.261
## 4        6    1000            191             809         0.191
## 5        7    1000            194             806         0.194
## 6        8    1000            134             866         0.134
## 7        9    1000            125             875         0.125
## 8       10    1000            104             896         0.104
## 9       11    1000            111             889         0.111
## 10      12    1000             87             913         0.087
## 11      13    1000             77             923         0.077
## 12      14    1000             81             919         0.081
## 13      15    1000             69             931         0.069
## 14      16    1000             71             929         0.071
## 15      17    1000             61             939         0.061
## 16      18    1000             58             942         0.058
## 17      19    1000             67             933         0.067
## 18      20    1000             46             954         0.046
## 19      21    1000             57             943         0.057
## 20      22    1000             48             952         0.048
## 21      23    1000             39             961         0.039
## 22      24    1000             42             958         0.042
## 23      25    1000             41             959         0.041
##    noChange_winTime noChange_lostTime noChangeWinRate
## 1               311               689           0.311
## 2               257               743           0.257
## 3               213               787           0.213
## 4               159               841           0.159
## 5               152               848           0.152
## 6               155               845           0.155
## 7               105               895           0.105
## 8               106               894           0.106
## 9                78               922           0.078
## 10               89               911           0.089
## 11               80               920           0.080
## 12               72               928           0.072
## 13               68               932           0.068
## 14               71               929           0.071
## 15               62               938           0.062
## 16               46               954           0.046
## 17               48               952           0.048
## 18               57               943           0.057
## 19               44               956           0.044
## 20               45               955           0.045
## 21               37               963           0.037
## 22               40               960           0.040
## 23               38               962           0.038
plot(df_result$changeWinRate, type = "l")
lines(df_result$noChangeWinRate, col="blue")