Advertisement
avogatro

world of tanks Christmas box gambling simulation 2022

Dec 5th, 2022 (edited)
2,043
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 7.50 KB | Gaming | 0 0
  1. #benchmark
  2. start.time <- Sys.time()
  3.  
  4. library(ggplot2)
  5. library(reshape2)
  6. #multi core
  7. library(future.apply)
  8. # more then 4 workers is slower
  9. plan(multisession, workers = 12)
  10. #################################
  11.  
  12. ntries <- 100000
  13. #25,28,75,80,
  14. nboxes <- c(200,400,600)
  15. #nboxes <- c(200)
  16. #################################
  17.  
  18.  
  19. results_high_tier_prop_table <- as.list(nboxes)
  20. results_low_tier_prop_table  <- as.list(nboxes)
  21. names(results_high_tier_prop_table) <- as.character(nboxes)
  22. names(results_low_tier_prop_table) <- as.character(nboxes)
  23.  
  24. # old game function only calc high tier prem
  25. # nbig_box number of big boxes
  26. # success_chance, is_safety_mechanic_active, npremium_tank_limit can be changed for the low tier tank calculation
  27.  
  28. game_simulation <- function(nbig_box, success_chance = 0.024,is_safety_mechanic_active = TRUE, npremium_tank_limit = 30){
  29.   npremium_tank <- 0
  30.   safety_mechanic_counter <- 50 # number of fails until guaranteed tank
  31.   rolls <- runif(nbig_box,0,1) # generate all random number at once is faster
  32.   for (roll in rolls){
  33.     if (roll <= success_chance){ # lucky
  34.       npremium_tank <- npremium_tank + 1
  35.       safety_mechanic_counter <- 50
  36.     } else { #unlucky
  37.       safety_mechanic_counter <- safety_mechanic_counter - 1
  38.      
  39.     }
  40.     if (isTRUE(is_safety_mechanic_active) & safety_mechanic_counter == 0){ # if very unlucky you get a prem
  41.       npremium_tank <- npremium_tank + 1
  42.       safety_mechanic_counter <- 50
  43.     }
  44.     if (npremium_tank >= npremium_tank_limit){
  45.       break
  46.     }
  47.   }
  48.   return (npremium_tank)
  49.  
  50. }
  51.  
  52.  
  53.  
  54. #only one of those events can happen at the same time
  55. #skin drop happens independent from those events, we don't care about skin here
  56. events = c("prem_day_1","prem_day_3","prem_day_7","gold_250","gold_500","gold_1000","credit_100k","credit_500k","low_tier_tank","high_tier_tank")
  57. event_chances = c(0.270,0.08,0.025,0.15,0.0494,0.015,0.20,0.070,0.1166,0.024 )
  58.  
  59. #main simulation
  60. #count all rewards we can get from nbig_box
  61. game_simulation2 <- function(nbig_box){
  62.   npremium_low_tier_tank <- 0
  63.   npremium_high_tier_tank <- 0
  64.   npremium_day <- 0
  65.   ngold <- nbig_box*250
  66.   ncredit <- 0
  67.  
  68.   safety_mechanic_counter <- 50 # number of fails until guaranteed tank
  69.   rolls <- sample(events, size = nbig_box, replace = TRUE, prob = event_chances)
  70.   for (roll in rolls){
  71.     if (roll == "high_tier_tank") {
  72.       npremium_high_tier_tank <- npremium_high_tier_tank + 1
  73.       safety_mechanic_counter <- 50
  74.     } else {
  75.       safety_mechanic_counter <- safety_mechanic_counter - 1
  76.    
  77.       if (safety_mechanic_counter == 0){ # if very unlucky you get a prem
  78.         npremium_high_tier_tank <- npremium_high_tier_tank + 1
  79.         safety_mechanic_counter <- 50
  80.       }else if(roll =="prem_day_1"){
  81.         npremium_day <- npremium_day + 1
  82.       }else if(roll =="prem_day_3"){
  83.         npremium_day <- npremium_day + 3
  84.       }else if(roll =="prem_day_7"){
  85.         npremium_day <- npremium_day + 7
  86.       }else if(roll =="gold_250"){
  87.         ngold <- ngold +250
  88.       }else if(roll =="gold_500"){
  89.         ngold <- ngold +500
  90.       }else if(roll =="gold_1000"){
  91.         ngold <- ngold +1000
  92.       }else if(roll =="credit_100k"){
  93.         ncredit <- ncredit+100000
  94.       }else if(roll =="credit_500k"){
  95.         ncredit <- ncredit+500000
  96.       }else if(roll =="low_tier_tank"){
  97.         npremium_low_tier_tank <- npremium_low_tier_tank+1
  98.       }
  99.     }
  100.    
  101.   }
  102.  
  103.  
  104.   result <- list()
  105.   result$npremium_low_tier_tank <- npremium_low_tier_tank
  106.   result$npremium_high_tier_tank <- npremium_high_tier_tank
  107.   result$npremium_day <- npremium_day
  108.   result$ngold <- ngold
  109.   result$ncredit <- ncredit
  110.   return (result)
  111.  
  112. }
  113.  
  114.  
  115. game_simulation_high_tier_tank <- function(nbig_box){
  116.   result <-game_simulation2(nbig_box)
  117.   return (result$npremium_high_tier_tank)
  118. }
  119.  
  120. game_simulation_low_tier_tank <- function(nbig_box){
  121.   result <-game_simulation2(nbig_box)
  122.   return (result$npremium_low_tier_tank)
  123. }
  124.  
  125. game_simulation_gold_no_prem <- function(nbig_box){
  126.   result <-game_simulation2(nbig_box)
  127.   return (result$ngold)
  128. }
  129. game_simulation_gold_with_prem <- function(nbig_box){
  130.   result <-game_simulation2(nbig_box)
  131.  
  132.   ngold <- result$ngold
  133.   ngold <- ngold + max(result$npremium_high_tier_tank-5,0)*(9100+9900+9500+9950+9850)/5
  134.   ngold <- ngold + max(result$npremium_low_tier_tank-5,0)*(750+850+1000+1500+1500)/5
  135.   return (ngold)
  136. }
  137.  
  138. ###############################
  139. #main function
  140. for (z in seq_along(nboxes)){
  141.   nbox <-nboxes[z]
  142.   print(nbox)
  143.   tries <- future_replicate(n=ntries,game_simulation2(nbox))
  144.   tries_mat <-matrix(unlist(tries), ncol=5,byrow=TRUE)
  145.   #print(attributes(tries_mat))
  146.   colnames(tries_mat)<- c("npremium_low_tier_tank","npremium_high_tier_tank","npremium_day","ngold","ncredit")
  147.   #print(tries_mat[1:9,c("npremium_low_tier_tank","npremium_high_tier_tank","npremium_day","ngold")])
  148.   result_high_tier_prop_table <- rep(0,30)
  149.   result_low_tier_prop_table <- rep(0,50)
  150.   #tb <- table(tries) / length(tries) # compute probabilities
  151.   high_tier_prop_table <- prop.table(table(tries_mat[,"npremium_high_tier_tank"]))
  152.   #tb <- rev(cumsum(rev(tb)))
  153.   result_high_tier_prop_table[as.integer(names(high_tier_prop_table)) + 1] <- high_tier_prop_table #tidy up
  154.  
  155.   results_high_tier_prop_table[[z]] <- result_high_tier_prop_table
  156.  
  157.   cat(nbox, "npremium_day", mean(tries_mat[,"npremium_day"]),"\n")
  158.   cat(nbox, "ncredit", mean(tries_mat[,"ncredit"]),"\n")
  159.   low_tier_prop_table <- prop.table(table(tries_mat[,"npremium_low_tier_tank"]))
  160.   result_low_tier_prop_table[as.integer(names(low_tier_prop_table)) + 1] <- low_tier_prop_table #tidy up
  161.  
  162.   results_low_tier_prop_table[[z]] <- result_low_tier_prop_table
  163. }
  164.  
  165. ##########################
  166. #output
  167. print("output")
  168. mat_high_tier <- as.data.frame(do.call(rbind, results_high_tier_prop_table))
  169. names(mat_high_tier) <- as.character(0:(ncol(mat_high_tier) -1))
  170. mat_high_tier <- cbind(rownames(mat_high_tier), mat_high_tier)
  171. names(mat_high_tier)[1] <- "nboxes"
  172. mat_high_tier <- mat_high_tier[1:25]
  173.  
  174. melted_high_tier <- melt(mat_high_tier, id.vars = "nboxes")
  175. melted_high_tier$nboxes <- factor(melted_high_tier$nboxes, levels = nboxes)
  176.  
  177.  
  178. mat_low_tier <- as.data.frame(do.call(rbind, results_low_tier_prop_table))
  179. names(mat_low_tier) <- as.character(0:(ncol(mat_low_tier) -1))
  180. mat_low_tier <- cbind(rownames(mat_low_tier), mat_low_tier)
  181. names(mat_low_tier)[1] <- "nboxes"
  182. mat_low_tier <- mat_low_tier[1:50]
  183. #print(mat_low_tier)
  184. melted_low_tier <- melt(mat_low_tier, id.vars = "nboxes")
  185. melted_low_tier$nboxes <- factor(melted_low_tier$nboxes, levels = nboxes)
  186.  
  187.  
  188. #draw
  189. p<-ggplot(data=melted_high_tier, aes(x=variable, y=value, fill=nboxes)) +
  190.   geom_bar(stat="identity", position = "dodge") +
  191.   labs(x = "Number of T8/9 premium", y = "Probability", fill = "Number of Boxes") +
  192.   scale_y_continuous(breaks = seq(0, 1, len = 21))
  193.  
  194. ggsave("high_tier_premium_tank.png", p)
  195.  
  196.  
  197.  
  198. p<-ggplot(data=melted_low_tier, aes(x=variable, y=value, fill=nboxes)) +
  199.   geom_bar(stat="identity", position = "dodge") +
  200.   labs(x = "Number of low tier premium", y = "Probability", fill = "Number of Boxes") +
  201.   scale_y_continuous(breaks = seq(0, 1, len = 21))
  202.  
  203. ggsave("low_tier_premium_tank.png", p)
  204.  
  205. #draw end
  206.  
  207. for (z in seq_along(nboxes)){
  208.   nbox <-nboxes[z]
  209.   #print(nbox)
  210.   ngold <- future_replicate(n=ntries,game_simulation_gold_with_prem(nbox))
  211.   cat("number of boxes:", nbox, "average gold: ", mean(ngold),"\n")
  212.   }
  213.  
  214.  
  215. #benchmarking end
  216. end.time <- Sys.time()
  217. time.taken <- end.time - start.time
  218. time.taken
  219. ###
Advertisement
Comments
  • avogatro
    1 year
    # text 0.63 KB | 0 0
    1. CMD output:
    2. Rscript wot_christmas_crates_properbility.R
    3. Loading required package: future
    4. [1] 25
    5. [1] 28
    6. [1] 75
    7. [1] 80
    8. [1] 200
    9. [1] 400
    10. [1] "output"
    11. Warning message:
    12. In (function (..., deparse.level = 1) :
    13. number of columns of result is not a multiple of vector length (arg 1)
    14. Saving 7 x 7 in image
    15. Saving 7 x 7 in image
    16. number of boxes: 25 average gold: 11054.67
    17. number of boxes: 28 average gold: 12436.65
    18. number of boxes: 75 average gold: 37250.61
    19. number of boxes: 80 average gold: 40106.91
    20. number of boxes: 200 average gold: 123806.5
    21. number of boxes: 400 average gold: 302540.4
    22. Time difference of 23.58386 secs
Add Comment
Please, Sign In to add comment
Advertisement