Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Rare Completion - ICRs and Unopened Packs
- #Written: 2021-0705 by Emil Karpinski
- #Setting up some variables
- #cards - tracks the cards that have been obtained in each method (no duplicates)
- #ItCount - tracks how many random ICRs were needed to complete each set (i.e. 1:[Rare_Amount] x )
- #Iteration - tracks how many times you've gotten an ICR (effectively a counter for rounds.)
- #i and a - general all purpose counters for the while and for loops
- #Rare_Amount - Integer denoting the amount of rares in the set
- #Rare_Obt - Integer denoting how many rares have already been obtained (used to seed [cards] during each simulation)
- #Simulations - The number of simulations to run.
- #Load in the dplyr library which we use to maintain the Col_Size dataframe.
- library(dplyr)
- #Defining some empty variables and setting some flags
- cards <- vector()
- ItCount <- vector()
- iteration <- 0
- i <- 0
- b <- 0
- Col_Size <- data.frame()
- Col_new <- data.frame()
- Col_time <- data.frame()
- It_Size <- vector()
- Uniq_It <- vector()
- Mean_Size <- vector()
- Mean_ICRTime <- vector()
- #Defining the model parameters
- Rare_Amount <- 60
- Rare_Obt <- 0
- Simulations <- 10000
- #Does simulations tracking the time needed to obtain a full set of rares (in full playsets, 4 of each).
- #Sets up the loop for the desired number of simulations (recommended 1k+)
- for (a in 1:Simulations)
- {
- #Runs until the set is complete - denoted by the flag (i) changing from 0 to 1
- while (i < 1)
- {
- #Seeding the collection with the currently owned amount of rares.
- #Checks if the amount of rares already obtained (Rare_obt is greater than 0 - i.e. you have something); and we haven't already gone through the loop.
- #If both satisfied, start populating the cards vector with your obtained rares. Does this in a while loop so you can control that you can't get >4 copies of a card.
- if ((Rare_Obt > 0) & (iteration == 0))
- {
- while (b < Rare_Obt)
- {
- x <- sample(1:Rare_Amount, 1, replace=T)
- if (sum(cards == x) < 4)
- {
- cards <- c(cards,x)
- b <- b+1
- }
- }
- b <- 0
- }
- #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.
- iteration <- iteration + 1
- #Samples a random number from 1:Rare_Amount (number of rares). Replace does nothing here.
- x <- sample(1:Rare_Amount, 1, replace=T)
- #Checks if the chosen card currently has less than 4 copies. If so then it adds that entry to the list of cards drawn (cards vector).
- #Then checks to see if the full set is complete (i.e. Rare_Amount*4 entries in that vector). If so it trips the i flag to leave the loop.
- if (sum(cards == x) < 4)
- {
- cards <- c(cards,x)
- #Records the iterations at which unique cards (i.e. those for which we don't have a playset are drawn)
- Uniq_It <- c(Uniq_It, iteration)
- if (length(cards) == (Rare_Amount*4))
- {
- i <- 1
- }
- }
- #Adds the amount of unique collected cards to a temporary vector (It_size) storing the size of the collection at each step of the iteration.
- It_Size <- c(It_Size, length(cards))
- }
- #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.
- ItCount <- c(ItCount, iteration)
- #Adds It_Size to Col_Size, to track how the size of the collection grew at each iteration within each simulation
- Col_Size <- bind_rows(Col_Size, as.data.frame(t(It_Size)))
- #Adds Uniq_It to Col_new, to track at which iterations we actually obtained a new unique cards across simulations
- Col_new <- bind_rows(Col_new, as.data.frame(t(Uniq_It)))
- #Cleans up the cards vector (sets it to empty), sets iterations to 0, and resets the flag to re-enable the while loop.
- cards <- vector()
- iteration <- 0
- i <- 0
- It_Size <- vector()
- Uniq_It <- vector()
- }
- #Doing some formatting on the Col_Size dataframe.
- #Replacing NA values with the max collection size (Rare_Amount*4)
- Col_Size[is.na(Col_Size)] <- (Rare_Amount*4)
- #Cleaning up the column names to strip out the V prefix.
- colnames(Col_Size) <- (c(1:max(ItCount)))
- colnames(Col_new) <- (c((Rare_Obt+1):(Rare_Amount*4)))
- #Calculating the average collection size at each iteration
- for (c in 1:max(ItCount))
- {
- Mean_Size <- c(Mean_Size, mean(Col_Size[,c]))
- }
- #Plot each simulation on the same graph and the mean size at each iteration as a line.
- plot(unlist(Col_Size[1,]), xlab = "Number of ICRs", ylab = "Collection Size", main = "Number of ICRs vs Collection Size")
- for (d in 2:Simulations)
- {
- points(unlist(Col_Size[d,]))
- }
- lines(Mean_Size, lwd = 3, col = "green")
- #Similar to the above, plots the number of ICRs needed to obtain the next rare within each simulation.
- #Also adds a line with the average across all simulations.
- for (e in 1:Simulations)
- {
- #Creates a temporary vector which stores the number of ICRs drawn from the previous unique rare to the next one
- temp <- c(0)
- #Computes the number of ICRs it took to get a new rare at each stage.
- for (f in 1:(length(Col_new)))
- {
- if (f==1)
- {
- temp <- c(temp, unlist(Col_new[e,f]))
- }
- else
- {
- temp <- c(temp, unlist(Col_new[e,f]-Col_new[e,(f-1)]))
- }
- }
- #Adds the temp vector to a new data.frame - Col_time
- Col_time <- bind_rows(Col_time, as.data.frame(t(temp)))
- }
- #Calculating the average time to next unique rare
- for (c in 1:length(Col_time))
- {
- Mean_ICRTime <- c(Mean_ICRTime, mean(Col_time[,c]))
- }
- #Plots the data on time to next unique rare. If this is the first time through the loop it creates the plot; else it adds to it.
- for (g in 1:Simulations)
- {
- if (g == 1)
- {
- plot(x = ((Rare_Obt):(Rare_Amount*4)), y = Col_time[g,], xlab = "Number of rares", ylab = "ICRs until next unique rare", main = "Time to Next Rare", ylim = c(0, Mean_ICRTime[((Rare_Amount*4+1)-(Rare_Obt))]*2))
- }
- else
- {
- points(x = ((Rare_Obt):((Rare_Amount*4))), y = Col_time[g,])
- }
- }
- lines(x = ((Rare_Obt):((Rare_Amount*4))), y = Mean_ICRTime, lwd = 3, col = "green")
Advertisement
Add Comment
Please, Sign In to add comment