8

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.

Community
  • 1
  • 1
Freesnöw
  • 30,619
  • 30
  • 89
  • 138
  • 4
    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... – Joe Oct 29 '11 at 17:51
  • Well, I'm asking mostly, what was the point of generating a fake GUID vs just using math.random to generate a name. It seems both have the same chances of creating the same filename. – Freesnöw Oct 29 '11 at 17:52
  • 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. – Joe Oct 29 '11 at 17:54
  • 7
    Someone wanted a thing that looked like a GUID, but was easier to get. Why do people buy fake Rolexes? – ObscureRobot Oct 29 '11 at 17:56
  • @Joe Post your comments (both) as a combined answer. It makes sense. – Freesnöw Oct 29 '11 at 18:25
  • 3
    There is no guarantee that a GUID is unique. A GUID is semi-unique. Even considering not-well-formatted GUID, there are only 2^128 possible GUID. Thanks to the Birthday Paradox, if you generate 2^64 of them you have like 50% of generating a duplicate. – xanatos Oct 29 '11 at 18:31
  • 1
    @xanatos: that is true, though 2^64 is eighteen quintillion and change... – Joe Oct 29 '11 at 19:16
  • 1
    @Joe Still it isn't "unique". You couldn't "sync" all the world on a single "unchecked" GUID. In the end a GUID isn't a **Globally Unique** ID. – xanatos Oct 29 '11 at 19:20
  • 1
    *I planned on using this to name file being uploaded to my server so that they didn't override each other.* --- I hope that JS is not running on the client side, because otherwise it means the client could also assign an arbitrary name to the uploaded file, a potential security risk. – kennytm Oct 29 '11 at 19:22
  • @xanatos: birthday paradox doesn't fully apply here, because GUIDs aren't just random numbers. There is a method to the algorithm, accounting for timestamp and source computer as well as bits to help avoid conflicts when those two values might be the same. – Joe Oct 29 '11 at 19:49
  • @Joe I'm very happy you think this, and I wish you good luck. But it's better you begin explaining this to the Microsoft Programmers that created SQL and .NET :-) If you read here, you'll discover that was true only for v1 GUID. http://en.wikipedia.org/wiki/Globally_unique_identifier – xanatos Oct 29 '11 at 19:50
  • @xanatos: oh, that's right, didn't they ditch the MAC address part to avoid identification issues? Best laid plans... – Joe Oct 29 '11 at 19:52
  • @Kenny this is a site that only about 15 people will be able access. If any of them had the ability to do that, they would be working with me on it. I'm having to make this because there all very computer illiterate. +1 a good point though. – Freesnöw Oct 29 '11 at 20:37

2 Answers2

4

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.

Joe
  • 41,484
  • 20
  • 104
  • 125
2

To make an example, even .NET GUID aren't "tied" to a machine. .NET uses v4 UUID for Guid.NewGuid() ( Simple proof that GUID is not unique ) so 122 bits of randomness plus 6 fixed bits. By reading here Are GUID collisions possible? it seems SQL Server uses a full random number (but I don't have an SQL Server to check... checked... It seems to use the same format of v4. There is a "fixed" 4 and the next "block" always begins with 8, 9, A, B. So 122 bits of randomness).

So what problem do you have with this random-generator that gives 128 bits of randomness in a "know" format? Would you prefer to see a non-fixed-format 128 bit number? Wow! Very practical... Here... Take one 43438471087229589138546501885363994076 (it's a GUID converted to its numericvalue, base 10). Now, how would you like to save it do your favourite DB? VARCHAR or NUMERIC? Isn't it easier to pack it in a GUID?

As a sidenote... What is the advantage over math.random()? Well... Over a single use of math.random() it's quite clear :-) I don't know how many bits of randomness a single call of math.random has, but at max it's 63 bits (a number in JS is 64 bits, but 1 bit is for the sign). And probably many many less.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280