TITLE: What's the probability of generating an existing account id?
QUESTION [6 upvotes]: I'm working on a website that supposed to generate a unique account id for every person that registers for the site. What is the probability of having the same user id being generated twice?

Each user id is $32$ characters in length.
Each character is randomly chosen and replaced from a set of characters
A set is a combination of [a-zA-Z] that is $52$ characters in length.

import secrets
from string import ascii_letters as letters


def get_random_user_id():
    while True:
        user_id = ''.join([letters[secrets.randbelow(len(letters))]
                            for _ in range(32)])
        try:
            User.objects.get(uid=user_id)
        except User.DoesNotExist:
            return user_id

That's my current function

def prob_of_col(n: int, set_size: int, output_size: int) -> float:
    '''Calculates the probability of a collision.

    Args:
        n: The number of users. 
        set_size: The number of characters to choose from.
        output_size: The number of characters to return. 
    Returns:
        A float that ranges from 0 to 1.
    '''

    e = 2.7182818284590452353602875
    return 1 - 1.0 / pow(e, ((n*(n-1))/(2*pow(set_size, output_size))))


n = pow(10, 27)
set_size = 52
output_size = 32

prob = prob_of_col(n, set_size, output_size)
print(prob)

REPLY [4 votes]: In cryptography, specifically hash collisions, there is a similar problem called Birthday Paradox. Your total "id space" is $52^{32}$. Obviously, by Pigeonhole principle, when the number of users is $>52^{32}$ (in a few centuries perhaps), then a collision exists or there are at least $2$ users with the same id. It's like asking to show that in a school with $367$ students at least $2$ have their birthday on the same day of the year.
But, if the number of users $N$ is $\leq 52^{32}$ then we can apply the following trick:

the $1$st user can take $52^{32}$ out of $52^{32}$ id's
the $2$nd user can take $52^{32}-1$ out of $52^{32}$ id's (one id is used by the $1$st user)
the $3$rd user can take $52^{32}-2$ out of $52^{32}$ id's (two id's are used by the previous $2$ users)
...
the $N$th user can take $52^{32}-(N-1)$ out of $52^{32}$ id's

This is the case when no collisions exist, with the probability of
$$P(\text{no collisions})=\frac{52^{32}}{52^{32}}\cdot
\frac{52^{32}-1}{52^{32}}\cdot\frac{52^{32}-2}{52^{32}}\cdot ...
\cdot\frac{52^{32}-(N-1)}{52^{32}}=\\
\frac{1}{(52^{32})^N}\cdot\frac{52^{32}!}{(52^{32}-N)!} \tag{1}$$
and 
$$P(\text{at least one collision exists})=1-P(\text{no collisions}) \tag{2}$$

For large $N$'s, $(1)$ can be further simplified using Stirling's approximation. I.e.
$$\color{red}{P(\text{at least one collision exists})}=1-P(\text{no collisions})\approx\\
1-\frac{1}{(52^{32})^N}\cdot
\sqrt{\frac{52^{32}}{52^{32}-N}}\cdot
\left({\frac{52^{32}}{e}}\right)^{52^{32}}\cdot
\left({\frac{e}{52^{32}-N}}\right)^{52^{32}-N}=\\
\color{red}{1-\frac{1}{e^N}\cdot
\left({\frac{52^{32}}{52^{32}-N}}\right)^{52^{32}-N+\frac{1}{2}}}$$

Update. For smaller $N$'s, those $N <<52^{32}$, we can apply the following approximation trick to $(1)$
$$e^x\approx 1+x \Rightarrow \frac{52^{32}-k}{52^{32}}=1-\frac{k}{52^{32}}\approx \frac{1}{e^{\frac{k}{52^{32}}}}$$
and
$$\color{blue}{P(\text{at least one collision exists})}=1-\prod\limits_{k=0}^{N-1}\left(1-\frac{k}{52^{32}}\right)\approx\\
1-\prod\limits_{k=0}^{N-1} \frac{1}{e^{\frac{k}{52^{32}}}}=
1-\frac{1}{e^{\sum\limits_{k=0}^{N-1}\frac{k}{52^{32}}}}=
\color{blue}{1-\frac{1}{e^{\frac{N(N-1)}{2\cdot52^{32}}}}}$$
For $N=10$ this is indeed a very small chance.