Probability of $n$ unique integers chosen randomly from $\{ 0, \ldots, n-1 \}$

118 Views Asked by At

For an array with range $n$ filled with random numbers ranging from 0 (inclusive) to $n$ (exclusive), what percent of the array contains unique numbers?

I was able to make a program that tries to calculate this with repeated trials and ended up with ~63.212%.

My Question is what equation could calculate this instead of me just repeating trials.

2

There are 2 best solutions below

0
On BEST ANSWER

Your number is suspiciously close to $1-1/e$. The fraction of values represented exactly $k$ times in your array should be close to $\exp(-1)/k!$, so it looks like your program counted the number of distinct values in the array, rather than the number of values represented only once.

0
On

If my interpretation is correct there are $n^n$ equiprobable array fillings, of which $n!$ have all numbers occurring at most once.

So you get as the answer: $$\frac{n!}{n^n}$$ and Stirling's approximation formula says that:

$$n! \sim n^{n+\frac{1}{2}}\sqrt{2\pi} e^{-n}$$ This should allow one to compute the limit of this fraction as $n$ gets larger.