Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shorter random string good enough "locally unique" identifiers?

It seems like many of the methods for producing GUIDs are pretty conservative, producing guids with an astronomically small chance of collision.

I’m wondering if, when you are producing key for a map of objects which are very unlikely to exceed 100K, it’s reasonable to use something other than GUID for a typical business application.

If for example, you use Math.random to select a series of 10 random characters from the set [A-Za-z0-9], that’s 62 character, so a string of 10 would create a domain of 62^10 (~8.3E14) possibilities. Even if I ended up needing 100M instead of 100K, the chance of a collision on the last id would be something like 1 in 8.3Bn. Isn’t that big enough if the consequences of a collision are not deadly or catastrophic?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(Also is "Math.random" random enough for these probabilities to hold?

>Solution :

The quick answer is that the gold standard that will suit any scenario is 128 bits of randomness, and you should always use Crypto.getRandomValues().

The Birthday problem describes the task of finding the probability of a collision.

Using the "square approximation" to calculate the possibility of a collision in your scenario, it turns out that your proposal has a 1 in 168 chance of a collision!

let n = Math.pow(10, 8)  // 100M objects
let m = Math.pow(62, 10) // 10 characters out of a character set of size 62
let p = n*n/(2*m)
console.log(1/p) // 1 in 168 chance of a collision!

You can try some different numbers for yourself. I’d suggest 15 characters, for a 1 in 153 billion chance of a collision.

let n = Math.pow(10, 8)  // 100M objects
let m = Math.pow(62, 15) // 15 characters out of a character set of size 62
let p = n*n/(2*m)
console.log(1/p) // 1 in 153 billion chance of a collision. Much better.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading