Imagine album for sticker collection, for example my daughter (Zuzia) has album with 192 empty enumerated places for (enumerated) stickers with Monster High creatures:-). You can buy stickers in foiled boosters, so you don’t know which sticker you draw. On the begining almost every sticker is new and you can put it in empty place (with the same number as on stick) in album. Later situation will become more difficult - often you already have this drawn card. On the end it’s hard to complete missing stickers.
The question is - how many stickers you need to buy to complete whole album with 192 empty places for 192 different stickers? Or rather how histogram looks like for many successful album completion?
This function will simulate filling in album with 192 empty (on the begining :-) ) places. It returns number of cars needed to compelte this action.
completeBook <- function() {
book <- rep(FALSE,192)
numIteration <-0
while(!all(book)){
book[sample(192,1)] <-TRUE
numIteration <- numIteration + 1
}
numIteration
}
Lets make simulation 100.000 times and collect results.
iterations <- rep(0,100000)
for(i in 1:100000) {
iterations[i]<-completeBook()
}
I made theoretical math investigatino for this problem. Mean value for general case “N empty fields o the begining can be estimated as:
N*(ln(N) +gamma + 1/(2N)), where gamma is Euler constant equal to 0.5772…
It means for N = 192 , mean number of needed cards will be 1121
Lets see outcom of our simulations
r mean(iterations)
## [1] 1121.302
Lets check also min and max values
r min(iterations)
## [1] 531
r max(iterations)
## [1] 3441
Now lets make the histogram