fork(2) download
  1.  
  2. # 6張一費, 6張二費, 5張3費
  3. cards <- c(rep(1, 6), rep(2, 6), rep(3, 5))
  4. # 補滿30張
  5. cards <- c(cards, rep("X", 30 - length(cards)))
  6. # 每張取名字
  7. names(cards) <- seq_along(cards)
  8.  
  9. # 起手一張二費與三張高費
  10. init.cards <- cards[c(7, 18, 19, 20)]
  11. # 牌庫剩下的牌。重洗的時候只會從這邊抽
  12. left.cards <- cards[setdiff(names(cards), names(init.cards))]
  13.  
  14. roger <- function() {
  15. # 換四張牌
  16. init.new <- sample(left.cards, 4, FALSE)
  17. # 燙兩次
  18. tom <- sample(cards[setdiff(names(cards), names(init.new))], 2, FALSE)
  19. # 第一回合的牌(前五張),與第二回合抽到的牌
  20. c(init.new, tom)
  21. }
  22.  
  23. shadesea <- function() {
  24. # 換三張牌保留二費
  25. init.new <- c(init.cards[1], head(sample(left.cards, 3, FALSE)))
  26. # 燙兩次
  27. tom <- sample(cards[setdiff(names(cards), names(init.new))], 2, FALSE)
  28. # 第一回合的牌(前五張),與第二回合抽到的牌
  29. c(init.new, tom)
  30. }
  31. checker <- list(
  32. has.1 = function(x) {
  33. "1" %in% head(x, 5)
  34. },
  35. has.2 = function(x) {
  36. "2" %in% head(x, 5)
  37. },
  38. has.12 = function(x) {
  39. ("1" %in% head(x, 5)) | ("2" %in% head(x, 5))
  40. },
  41. # 2+2
  42. has.2.2 = function(x) {
  43. sum(x == "2") >= 2
  44. },
  45. # 1+2
  46. has.1.2 = function(x) {
  47. ("1" %in% head(x, 5)) & ("2" %in% x)
  48. },
  49. has.2.1 = function(x) {
  50. ("2" %in% head(x, 5)) & ("1" %in% x)
  51. },
  52. has.2.11 = function(x) {
  53. ("2" %in% head(x, 5)) & (sum("1" == x) >= 2)
  54. },
  55. has.1.1 = function(x) {
  56. sum(x == "1") >= 2
  57. },
  58. has.1.11 = function(x) {
  59. sum(x == "1") >= 3
  60. },
  61. has.11.11 = function(x) {
  62. sum(x == "1") >= 4
  63. },
  64. has.1.3 = function(x) {
  65. ("1" %in% head(x, 5)) & ("3" %in% x)
  66. },
  67. has.1.12 = function(x) {
  68. ("1" %in% head(x, 5)) & (sum(x =="1") >= 2) & ("2" %in% x)
  69. },
  70. # 滿水晶
  71. crystal.full = function(x) {
  72. checker$has.1.2(x) | checker$has.2.2(x) | checker$has.1.11(x) | checker$has.11.11(x) | checker$has.1.3(x) | checker$has.1.12(x)
  73. },
  74. # 有做事
  75. no.idle = function(x) {
  76. checker$crystal.full(x) | checker$has.2.1(x) | checker$has.1.1(x)
  77. }
  78. )
  79. tasks <- expand.grid(sample = c("roger", "shadesea"), fname = names(checker))
  80. task.name <- apply(tasks, 1, paste, collapse = "-")
  81. result <- sapply(1:20, function(i, n) {
  82. sample <- list(
  83. roger = lapply(seq_len(n), function(i) roger()),
  84. shadesea = lapply(seq_len(n), function(i) shadesea())
  85. )
  86. result <- apply(tasks, 1, function(x) {
  87. mean(sapply(sample[[x[1]]], checker[[x[2]]]))
  88. })
  89. names(result) <- task.name
  90. result
  91. }, n = 100) # 免費的計算資源有限,所以調降樣本數
  92. # result
  93. result2 <- apply(result, 1, function(x) {
  94. c(mean(x) - 2 * sd(x), mean(x), mean(x) + 2 * sd(x))
  95. })
  96. rownames(result2) <- c("95% Confidence Interval(lower)", "Estimated Probability", "95% Confidence Interval(upper)")
  97. result2
  98.  
  99.  
  100.  
  101.  
Success #stdin #stdout 1.12s 178496KB
stdin
Standard input is empty
stdout
                               roger-has.1 shadesea-has.1 roger-has.2
95% Confidence Interval(lower)   0.6921067      0.5691964   0.6118909
Estimated Probability            0.7475000      0.6570000   0.7005000
95% Confidence Interval(upper)   0.8028933      0.7448036   0.7891091
                               shadesea-has.2 roger-has.12 shadesea-has.12
95% Confidence Interval(lower)              1    0.9151369               1
Estimated Probability                       1    0.9475000               1
95% Confidence Interval(upper)              1    0.9798631               1
                               roger-has.2.2 shadesea-has.2.2 roger-has.1.2
95% Confidence Interval(lower)      0.235988        0.5496737     0.4745913
Estimated Probability               0.339500        0.6585000     0.5560000
95% Confidence Interval(upper)      0.443012        0.7673263     0.6374087
                               shadesea-has.1.2 roger-has.2.1 shadesea-has.2.1
95% Confidence Interval(lower)        0.5691964     0.4533297        0.6351693
Estimated Probability                 0.6570000     0.5550000        0.7400000
95% Confidence Interval(upper)        0.7448036     0.6566703        0.8448307
                               roger-has.2.11 shadesea-has.2.11 roger-has.1.1
95% Confidence Interval(lower)      0.1664693         0.2226217      0.288797
Estimated Probability               0.2310000         0.3010000      0.386000
95% Confidence Interval(upper)      0.2955307         0.3793783      0.483203
                               shadesea-has.1.1 roger-has.1.11
95% Confidence Interval(lower)        0.2226217     0.05122609
Estimated Probability                 0.3010000     0.09450000
95% Confidence Interval(upper)        0.3793783     0.13777391
                               shadesea-has.1.11 roger-has.11.11
95% Confidence Interval(lower)      0.0002589016     -0.01487419
Estimated Probability               0.0555000000      0.01100000
95% Confidence Interval(upper)      0.1107410984      0.03687419
                               shadesea-has.11.11 roger-has.1.3
95% Confidence Interval(lower)       -0.009226655     0.4404331
Estimated Probability                 0.004500000     0.5195000
95% Confidence Interval(upper)        0.018226655     0.5985669
                               shadesea-has.1.3 roger-has.1.12
95% Confidence Interval(lower)        0.3267662      0.1895583
Estimated Probability                 0.4210000      0.2630000
95% Confidence Interval(upper)        0.5152338      0.3364417
                               shadesea-has.1.12 roger-crystal.full
95% Confidence Interval(lower)         0.2226217          0.7896552
Estimated Probability                  0.3010000          0.8460000
95% Confidence Interval(upper)         0.3793783          0.9023448
                               shadesea-crystal.full roger-no.idle
95% Confidence Interval(lower)             0.8602767     0.8418847
Estimated Probability                      0.9190000     0.8865000
95% Confidence Interval(upper)             0.9777233     0.9311153
                               shadesea-no.idle
95% Confidence Interval(lower)        0.8890746
Estimated Probability                 0.9415000
95% Confidence Interval(upper)        0.9939254