Should I have a repeated digit in a $4$-digit pin code?

A small argument my colleague and I were having...

Say I've got a $4$-digit security code on my burglar alarm, and I'm too lazy to ever change it. Over time, the digits involved will become worn.

If I've used $4$ different digits (as is common, eg $1357$), then an attacker, knowing those are the digits in the code, needs (if I'm right!) $24$ goes to try all combinations.

If I've repeated a digit (eg, $1317$), the attacker just knows $1$, $3$ and $7$ are used (assume that all wearing is indistinguishable). How many different possible $4$-digit codes are possible, using each digit at least once? I say it's more than $24$, my colleague is convinced otherwise.

Bonus kudos for generalizing the problem to knowing $k$ digits in an $n$-digit pin. ;)


If the burglar knows which digit is repeated, (s)he has \begin{equation*} \dfrac{4!}{2!} = 12 \end{equation*} codes to try. Since (s)he doesn't know which digit is repeated, (s)he will have to try $3 \cdot 12=36$ different codes.


I really like that problem, it is very visual and easy to understand. I'll show the case of a code lenght of $n$ where $l<n$ digits are repeated exactly twice. That makes for a total of $k = n-l$ different digits. (i.e. for your particular question please set $n=4$, $l=1$.)

Given $n$ different digits without repetition, you got $n!$ ways to order them. That is a fairly basic problem.

Now assume you still have $n$ digits, but $l$ „marked“ ones are repeated twice. (i.e. the attacker knows which digit you double) This will reduce your possibilities by a factor of $2^k$.

Why? Your set of numbers looks like $\{1_1,1_2,2_1,2_2,...,l_1,l_2,l+1,l+2,...,n-l=k\}$. That is still $n$ elements, because the first $l$ are doubled. When ordering those we have $n!$ choices, because we can see those little indices. But you burglar alarm can't, we are counting to many possibilities! We need to figure out how often each possibility was counted and adjust for this. Each pair of doubled numbers $j_1, j_2$ (here $j\leq l$) could be placed as $..,j_1,..,j_2,..$ or the other way round as $..,j_2,..j_1,..$ giving you two possibilities in total. As changing that around is independent we need to multiply all those twos together.

The final step now considers the burglar, who doesn't know which digits are doubled. He will have to try each possible subset with $l$ elements. This is known as the binomial coefficient $n\choose l$. In total this gives us:

$${n \choose l} \cdot \frac{n!}{2^l}$$

Note that $2^l$ grows faster than $n \choose l$, but the later start with a higher initial amount. In conclusion: A small number of doubled keys is sensible, depending on your $n$. But make sure you don't double to many!