Guest User

Playset_Drawing_Testing.R

a guest
Jul 8th, 2021
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.36 KB | None | 0 0
  1. #Playset Drawing Simulations (to determine how best to model ICR chances for rare playsets)
  2. #Written: 2021-0704 by Emil Karpinski
  3.  
  4. #Setting up some variables
  5. #cards - tracks the cards that have been obtained in each method (no duplicates)
  6. #ItCount - tracks how many random ICRs were needed to complete each set (i.e. 1:100 x 4 in the Four_Off method; 1:400 x 1 in the Individual Card method)
  7. #Iteration - tracks how many times you've gotten an ICR
  8. #i and a - general all purpose counters for the while and for loops
  9.  
  10. cards <- vector()
  11. ItCount <- vector()
  12. iteration <- 0
  13. i <- 0
  14.  
  15. #Does 1000 simulations tracking the time needed to obtain a set of 100 rares (in full playsets, 4 of each). This is done two ways.
  16. #The first way simulates more closely the actual game. It requires you to collect 4x 100 rares, represented by the numbers 1:100 (i.e. requires four instances of the value 1, four instances of the value 2, ..., four instances of the value 100).
  17. #The second way treats each rare as unique; so the first rare playset requires values 1, 2, 3, and 4. This way is easier to code, but takes longer.
  18.  
  19. #Sets up the loop for the 1000 simulations
  20. for (a in 1:10000)
  21. {
  22.   #Runs until the set is complete - denoted by the flag (i) changing from 0 to 1
  23.   while (i < 1)
  24.   {
  25.     #Counts how many times we've been through this loop. i.e. how many times have we drawn a card and checked if we have it     or not.
  26.     iteration <- iteration + 1
  27.    
  28.     #Samples a random number from 1:100 (where 100 is the number of rares). Replace does nothing here.
  29.     x <- sample(1:100, 1, replace=T)
  30.    
  31.     #Checks if the chosen number has been drawn less than 4 cards. If so then it adds that entry to the list of cards drawn (cards vector).
  32.     #Then checks to see if there are 400 entries in that vector (which would denote a full playset of all 100 rares). If so it trips the i flag to leave the loop.
  33.     if (sum(cards == x) < 4)
  34.     {
  35.       cards <- c(cards,x)
  36.      
  37.       #Records the iterations at which unique cards (i.e. those for which we don't have a playset are drawn)
  38.      
  39.      
  40.       if (length(cards) == 400)
  41.       {
  42.         i <- 1
  43.       }
  44.     }
  45.  
  46.   }
  47.  
  48.   #Extends the vector ItCount (stores the number of draws it took to get a full playset - i.e. iteration), and adds the last value of iteration to it.  
  49.   ItCount <- c(ItCount, iteration)
  50.  
  51.   #Cleans up the cards vector (sets it to empty), sets iterations to 0, and resets the flag to re-enable the while loop.
  52.   cards <- vector()
  53.   iteration <- 0
  54.   i <- 0
  55. }
  56.  
  57. #Since I copied and pasted the above in the below version, I'm just creating a new vector here (Four_Off_Comp_Count) to store the values within ItCount so I don't need to Ctrl+F and replace it in the loops below. Then wipe ItCount so it can be used in the second batch of simulations.
  58. Four_Off_Comp_Count <- ItCount
  59. ItCount <- vector()
  60.  
  61. #Sets up the loop for 1000 simulations
  62. for (b in 1:10000)
  63. {
  64.   #As before
  65.   while (i < 1)
  66.   {
  67.     #As before, but not sampling from the numbers 1:400 with sets of 4 representing a playset.
  68.     x <- sample(1:400, 1, replace=T)
  69.    
  70.     #As before, but checks if that number is present. If it isn't it's added, and we check to see all 400 unique entries are present. If so the i flag gets set     to 1 to escape the loop.
  71.     if (sum(cards == x) < 1)
  72.     {
  73.       cards <- c(cards,x)
  74.      
  75.       if (length(cards) == 400)
  76.       {
  77.         i <- 1
  78.       }
  79.     }
  80.     #Regardless iterations are increased by 1
  81.     iteration <- iteration + 1
  82.   }
  83.  
  84.   #Stores the number of cards you needed to draw to get a complete set of 400 rares.
  85.   ItCount <- c(ItCount, iteration)
  86.  
  87.   #Resets everything like before
  88.   cards <- vector()
  89.   iteration <- 0
  90.   i <- 0
  91.  
  92. }
  93. #For consistencies sake, also outputting the values in ItCount to a vector for this method, then wiping it.
  94. Ind_Card_Comp_Count <- ItCount
  95. ItCount <- vector()
  96.  
  97. #outputs some basic summary stats for the two approaches.
  98. summary(Four_Off_Comp_Count)
  99. summary(Ind_Card_Comp_Count)
  100.  
  101. plot(Four_Off_Comp_Count, type = "p", xlab = "Simulation Number", ylab = "Iterations", main = "Draws until Rare Completion", col = "blue", ylim = c(400,6000))
  102. points(Ind_Card_Comp_Count, col = "red")
  103.  
  104. lines(c(c(1:10000)), rep(mean(Four_Off_Comp_Count), 10000), col = "yellow", lwd = 3)
  105. lines(c(c(1:10000)), rep(mean(Ind_Card_Comp_Count), 10000), col = "green", lwd = 3)
  106.  
Advertisement
Add Comment
Please, Sign In to add comment