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.
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.