I was having a chat with my friend. We were discussing the problem of producing an array of size $N$ filled with random numbers from $0$ to $N - 1$.
Here by filled with random numbers from $0$ to $N - 1$ , I mean,
- Every number in the array should have been randomly selected with an equal probability of $1/N$
I suggested this,
- Assuming I have a way to uniformly generate random number between $[0, 1)$, say $Rnd()$
- Fill each element in the array as, $floor(Rnd() * N)$
My friend said this,
- Assuming I have a way to shuffle the array.
- Fill the array with numbers $0 .. N - 1$ at the indices $0 .. N-1$
- Do a shuffle on the array.
My friend claims that his method is better because it ensures that every number between $0 .. N - 1$ will occur once and only once. Hence stratifying, the equal probability requirement.
Another difference that I see here is that in the way I suggested, it is possible for a number to be chosen 2 times whereas this is not possible in my friend's way. So the results each of our methods produce are different.
Given that we just have to produce an array of size $N$ which can only contain numbers between $0 .. N-1$ and the probability of occurrence of any number is equal, $1/N$. I couldn't find anything wrong with either of these methods though they are different and produce different results.
Who here, if any, is wrong and why ? Or are we both wrong ?