Number of unique integer in random generated arrays

244 Views Asked by At

I generated 1.000.000 random integers in the range from 0 to 1.000.000 (using rand() in c++ and random.randrange() in python) and both code got approximately 632000 unique numbers.

I noticed $1 - e^{-1} = 0.6321$.

Does $e$ involve in this? If so, why?

Unique number | Total numbers in array
632413           1000000

632088           1000000

631594           1000000

6305             10000

6319             10000

1

There are 1 best solutions below

4
On BEST ANSWER

Let $N$ be a large number (in your case, $N=10^6$).

By linearity of expectation, we define an indicator for each number from $1$ to $N$ according to whether or not it appears.

The probability that a number appears is $$1-\left(\frac {N-1}{N}\right)^{N}$$

Hence your expectation is $$N\left(1-\left(\frac {N-1}{N}\right)^{N}\right)$$

Now, $$\lim_{N\to \infty} \left(\frac {N-1}N\right)^N=1-e^{-1}$$

So your expectation is approximately $$N\times (1-e^{-1})$$ as desired.