You are faced with a problem called a birthday: even if there are 366 possible birthdays, when you get only 26 people in the room, the likelihood that some couple will have the same birthday will be better than 50-50. In the general case, collisions are likely when your numbers approach the square root of the sample size (26 is in the vicinity of the square root of 366).
Javascript Math.random () has about 52 bits of randomness. Therefore, collisions should be probable, as the number of your records is approaching 2 ** 26, which is about 60 million, and this is a rather modest size for the database.
You must use a cryptographically secure PRNG with at least 128 bits, preferably 256, to avoid collisions. Perhaps, ready-to-use UUID libraries are available for this.
For a given number of keys k and key space N, approximate collision factors:
1 - exp ((- k * (k-1)) / (2 * N))
So, for k = 1 million entries, N = 2 ** 52, which is about 1 in 9000 if I did the math correctly. This also assumes that Javascript Math.random () does indeed use the fill of 52 bits of randomness available to it ... this is also an assumption I would not make.
Lee Daniel Crocker Jan 29 '15 at 17:29 2015-01-29 17:29
source share