4.6 야구카드 모으기

총 100종류의 서로 다른 야구카드가 있다고 하자. 어떤 사람이 한 장 한 장을 무작위로 구매할 때 평균적으로 총 몇 장을 구매해야 100종류의 카드를 모두 모으겠는가 하는 문제이다.

k = 100  # kinds of cards
N = 1000 # number of simulation
Res = rep(NA, N)

t1 = Sys.time()
for (i in 1:N) {
    nCard = rep(0, k) # 0: Do not have, 1: Have
    nT = 0            # number of trial
    while (sum(nCard) < k)  {
      nCard[sample(k, 1)] = 1
      nT = nT + 1
    }
    Res[i] = nT
}
Sys.time() - t1
Time difference of 1.578759 secs
mean(Res)
[1] 524.647
summary(Res)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  297.0   432.0   504.0   524.6   594.2  1202.0 
hist(Res, breaks=1:max(Res), xlab="Purchase count")
Histogram of purchases

Figure 4.9: Histogram of purchases

이 문제의 이론적인 답은 약 518.73775 이다.