DEV Community

Discussion on: How to Generate Unique ID in JavaScript

Collapse
 
peledzohar profile image
Zohar Peled • Edited

A random number is not the same as a unique number. There is absolutely no guarantee of uniqueness in a collection of randomly generated numbers.

DateTime.Now() will also not generate a unique number, unless guaranteed not to run at the exact same millisecond. for quite a while now, computers are fast enough to run a few iteration of a loop within a single millisecond.

Collapse
 
mitchneutron profile image
Mitchell Mortensen

While yes it's theoretically possible for UUID to produce a duplicate, it is statistically improbable (read: impossible) to do so, even when striving for such and taking into account the birthday paradox. According to wikipedia:

"Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. Or, to put it another way, the probability of one duplicate would be about 50% if every person on earth owned 600 million UUIDs."
en.wikipedia.org/wiki/Universally_...

You actually more likely to have a totally unique ID than to have gotten one that anyone has ever seen in the entire world! And it will be this way until the sun runs out of juice.

UUID is the only one that should be used. Date.now() is terrible for reasons that Zohar mentions.

Collapse
 
rahmanfadhil profile image
Rahman Fadhil

I kind of agree 🤔

I think the title should be "how to generate random id"

But, at least I put UUID as the most recommended approach

Collapse
 
peledzohar profile image
Zohar Peled

In fact, random numbers probability of uniqueness is actually a lot smaller than what one can expect, without knowledge of what's called the birthday problem.

In a nutshell - Take a group of random people. What are the chances two of them was born in the same date (day and month, year not included)?

Turns out, that if the group contains 32 people, that chance is 50%. With 70 people in the group, its 99.9%.

So the chances of getting the same random number twice when your range is between 0 and 100 increase rapidly the larger the group is.

Collapse
 
siddharthshyniben profile image
Siddharth

I just tried the Date.now() method, and even when I ran a loop 1000 times, all the "unique" numbers were the same.

Collapse
 
snakebite382 profile image
Caelin Limerick

That's because it all ran within the same millisecond. Date.now() returns time since epoch in ms, so if you run it twice within 1ms the number will be the same since the time since epoch in ms is the same. This is why UUID is a much better approach (like literally the best, universally agreed on approach) however you can still fix this by adding a 1ms delay in your loop, or doing something like multiply by math.random() to add some varation.

Thread Thread
 
siddharthshyniben profile image
Siddharth

Yeah that's right!

Also, UUID is slowly being replaced by NanoID - it's faster, smaller (both package size and IDs), URL friendly, and cryptographically more secure (although both of them are secure enough for that to be irrelevant)

Collapse
 
forkbomb profile image
Joe Buckle

UUID is the goto solution for JS at least.

Collapse
 
coderkearns profile image
coderkearns

You could decrease the chances by using multiple of the author's ways:

Maybe

function uniqueID() {
return Math.floor(Math.random() * Date.now())
}

You could even string multiple randoms together:

Math.floor(Math.random() * Math.floor(Math.random() * Date.now()))

Collapse
 
wolffe profile image
Ciprian Popescu

Depending on the application, this is the right way to do it. In my case, I don't have a loop, I have several elements being displayed on subsequent clicks, and there's several seconds between each click. So there's zero chances of an identical ID.

Collapse
 
abbatyya profile image
abbatyya

thank you, this work also. to also include string i use Math.floor(Math.random() * Date.now()).toString(16)