Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes this pseudo-GUID generator better than math.random()?

I found the question here:

Create GUID / UUID in JavaScript?

The answer provides the following JS:

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function guid() {
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

Now, Some of this seems silly to me. Why so much repetition? I planned on using this to name file being uploaded to my server so that they didn't override each other. This doesn't look like it will always generate a unique number.

What is the above codes benefit over just naming the file math.random(). It doesn't even change the seed.

Sorry, I've never worked with GUID / UUID ever and some of the code doesn't really make any sense to me...

CLARIFICATION

A lot of people aren't answering the question like I asked it. A lot of people are explaining that GUID isn't always unique, blah blah blah. That isn't what I'm asking. I'm asking, what was the point of using it over just math.random().

Joe seems to have given the best answer for me in the comments.

like image 825
Freesnöw Avatar asked Oct 29 '11 17:10

Freesnöw


People also ask

Why is math random pseudo-random?

Algorithmic random number generation can't exactly be random, per se; which is why they're more aptly called pseudo-random number generators (PRNGs). If you're using math and formulae to create a sequence of numbers, random though they might seem, those numbers will eventually repeat and reveal a non-random pattern.

Is GUID guaranteed to be unique?

While each generated GUID is not guaranteed to be unique, the total number of unique keys (2128 or 3.4×1038) is so large that the probability of the same number being generated twice is very small.

How random is GUID?

How unique is a GUID? 128-bits is big enough and the generation algorithm is unique enough that if 1,000,000,000 GUIDs per second were generated for 1 year the probability of a duplicate would be only 50%. Or if every human on Earth generated 600,000,000 GUIDs there would only be a 50% probability of a duplicate.

Is GUID truly random?

Definitely not random. Similarly, the person who wanted to use a GUID for password generation would find that the passwords are totally predictable if you know what time the GUID was generated and which computer generated the GUID (which you can get by looking at the final six bytes from some other password-GUID).


1 Answers

Even that answer says: "do you want actual GUIDs, or just random numbers that look like GUIDs?" Because those aren't real GUIDs. Also from the original thread: "There's no way to generate real GUIDs in Javascript, because they depend on properties of the local computer that browsers do not expose." so my question is: do you need a GUID? Or just a random filename? There's nothing magical about a GUID as a consumer, it is not how it looks, it's how it's generated. For a random filename, using clock ticks + a random number would be (at least as) effective...

In your case, no reason. But if you have JS code that is talking to something that is expecting a GUID or something in that form, you would need to pass something of a similar format since you can't generate the real thing in pure JS.

like image 72
Joe Avatar answered Sep 29 '22 03:09

Joe