Yes. As long as there's a limit on numeric precision, a random number algorithm always has the possibility of collision (generating two identical values).
JavaScript's Math.random()
function returns a random number equal to 0 <= N < 1
. In the real world, N
is theoretically infinite. In computing, the limited precision of the result of any random()
function results in a finite result set.
JavaScript uses signed 64-bit doubles, but the random()
function doesn't return negative values. So the maximum range of unique return values is equivalent to a 32-bit unsigned integer.
Therefore the odds of Math.random() === Math.random()
evaluating to true are around 1 in 4294967296^2, or 1 in 1.8e19, or 1 in 18 quintillion.
For this to happen in practice, it would require the function running in a loop and executing one billion times per second ( 1 GHz ) for around 500 years. Or you could get lucky on your first try. ;-)