Post

Collecting Verification Codes (1)

Collecting Verification Codes (1)

Introduction

When I try to log into my university’s SSO system, it gives me a two-digit verification code that I must select on my cellphone. For instance, in the images below, the website (on the left) shows the number 25, and I would tap 25 on my phone (on the right). Now, I’m curious about how many attempts I would need to make to see all possible 100 codes.

smart-login-web smart-login-phone

Problem Statement

Let S be a finite set of size N. At the beginning, a set F is initialized to be an empty set. For each step, a random element r is chosen at uniformly random from S and put to F, i.e., FF{r}. Then, what is the expected number of steps to reach S=F?

Now, take your time to solve this problem! (Hint: it is easy.)

Solution

Solution 1

For fixed N, suppose there are n elements should be chosen to reach S=F. In other words, n=|SF|. Now, let an be the expected number of additional steps at the moment. We let a0=0.

Then, we have n/N probability of getting a new fresh number and 1n/N probability of getting a number that has been chosen before. Thus, noticing that we need this one step to choose an element, we have the following recursive formula:

an=1+nNan1+(1nN)an

Rearranging, we get

an=an1+Nn.

Equation (???) directly gives

an=Ni=1n1i=NHn

where Hn is the n-th harmonic number.

In particular, we have aN=NHN. (In fact, this solution needs more verification that each ai exists but I omit.)

Solution 2

For fixed N, let Xi be a random variable for the number of steps until the next element which has not been chosen before when there are i elements not chosen. Then, we immediately get E[Xi]=N/i. Then, X, the random variable for the number of steps to reach F=S equals i=1NXi; by linearity, we get

E[X]=i=1NE[Xi]=Ni=1N1i=NHN.

Conclusion

Hence, as 100H100518.74, I shall expect about 519 log-in tries to observe all 100 numbers.

This post is licensed under CC BY 4.0 by the author.