Comment On When once just isn't enough ...

... then call a function from an external library that does exactly the same thing. That's what Mark Pitman's colleague does. If you think about it, doing this wille ensure that your GUID is doubly-random. And if you're really lucky, it'll be randomly-random. Even better!  [expand full text]
« PrevPage 1Next »

re: When once just isn't enough ...

2004-10-01 13:57 • by Katrina
Hey Alex,

I just wanted to say Hi and your blog is awesome!!Love you!

re: When once just isn't enough ...

2004-10-01 14:20 • by Dave Mays
From the department of redundancy department....

And doesn't this guy realise he's using up twice as many GUIDs this way? It's really going to suck when we all run out early because of him. ;-)

re: When once just isn't enough ...

2004-10-01 14:32 • by Patrick
Also importing a DLL and calling unmanaged code, from managed code, simply to do something that was already done!

re: When once just isn't enough ...

2004-10-01 14:33 • by Jacques Troux
WON'T SOMEONE THINK OF THE GUIDS???!!!

re: When once just isn't enough ...

2004-10-01 14:56 • by cicorias
This compares, but is far worse, to code I saw where the deveoper called out to SQL Server to generate a new guid with

select newid()

WTF!!! I flipped

re: When once just isn't enough ...

2004-10-01 15:04 • by icelava
Unmanaged GUID > managed GUID

is what the programmer wants to say.

re: When once just isn't enough ...

2004-10-01 15:35 • by Derick Bailey
oo! oo! i'll play "defender of the WTF" for this round! :D

Does anyone know if the Guid.NewGuid() method actually returns a truely unique guid based on the network card MAC address and all that jazz, the same way CoCreateGUID does? If NewGuid doesn't do the external call for us, or at least run the same algorhythm the create the guid, then the API call may be justified here.

re: When once just isn't enough ...

2004-10-01 15:36 • by Derick Bailey
but either way... this is still stupid code since he wouldn't need to call GUid.NewGuid... .NET's GUID is a STRUCTURE which does not need to be explicitly instantiated.

re: When once just isn't enough ...

2004-10-01 15:54 • by Rojohn
Yes, NewGuid in fact calls CoCreateGuid. But CoCreateGuid calls UuidCreate, which does not use the MAC address these days.

re: When once just isn't enough ...

2004-10-01 16:05 • by Rojohn
Oh and I say blame the code on MSDN. The doc is a bit vague when it says: "Initializes a new instance of the Guid class." What the heck does 'initialize' mean? So this guy probably just didn't want to use any undocumented features. ;-)

re: When once just isn't enough ...

2004-10-01 16:09 • by Hassan Voyeau
I agree, the only way I would defend this is if the ole32 GUID algorithm is somehow different from the .NET GUID algorithm.

re: When once just isn't enough ...

2004-10-01 16:13 • by Hassan Voyeau
[Append to my last comment] : Can anyone say for certain that this is not the case?

re: When once just isn't enough ...

2004-10-01 16:18 • by Dave Mays
And even if the algorithms were different between the Ole32 and .NET Guid creator, what would possibly be the justification for caring how your GLOBALLY UNIQUE identifier was created? Who cares if it's based (partially) on a MAC address??

re: When once just isn't enough ...

2004-10-01 16:21 • by Dave Mays
And, from MSDN:

The full .NET Framework Guid.NewGuid method calls the Windows API function CoCreateGuid that calls UuidCreate to generate globally unique 128-bit numbers.

( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/PPCGuidGen.asp )

re: When once just isn't enough ...

2004-10-01 16:32 • by Jon Galloway
Dave, you're just not thinking Enterprise Development. Sure, Globally Unique is fine _for_now_, but what about when our applications are running on other planets or stars or whatever? That's why we need Universally Unique ID's (UUID's), and there's no UUID class in the .NET.

re: When once just isn't enough ...

2004-10-01 16:40 • by Hassan Voyeau
Using two different algorithms makes it possible to create a GUID twice. However, as shown in your quote from MSDN, this is not the case, making this a WTF indeed.

re: When once just isn't enough ...

2004-10-01 16:51 • by Scott
No, using two different algorithms doesn't make it any more possible to create two of the same globally _unique_ identifiers.

re: When once just isn't enough ...

2004-10-01 16:53 • by Scott
Sorry for the double post, but the algorithm is encoded into the guid. Making it actually impossible to create two of the same guids with two different algorithms.

re: When once just isn't enough ...

2004-10-01 17:02 • by Patrick
Scott, sorry but thats nonsense. A million monkeys pounding at a keyboard could duplicate your "unique" GUID, given enough time.

It's just that the chances of a GUID clash are VERY, VERY remote, given the amount of digits in the GUID.

re: When once just isn't enough ...

2004-10-01 17:15 • by Ludvig A. Norin
Come to think of it, I'd say Scott is correct. A proper GUID (or UUID for that matter) must have an algorithm identifier. If it's not, it's not a GUID by definition - rather it's just a (possibly) random 128-bit number. Just making up a 128bit number won't get you a GUID, right? I still wonder if there's a difference betweed UUID and GUID's though (by specification that is). GUID's are obviously UUID's, but is vice versa true as well?

re: When once just isn't enough ...

2004-10-01 17:52 • by Scott
UUID is actually another term for a GUID.

Guids are made up of different fields to help guarantee uniqueness.

Typically they are: algorithm, spacial identifier (on the mac address based algorithms this would be something involving the mac address), and another section involving the spacial identifier and the time or something involving clock cycles.

So, really, if a million monkeys were generating guids if they were to generate a clash it would be a clash with themselves probably. They would most likely die before this would happen making the guids REASONABLY unique. 128 bits is a HUGE space to deal with.

re: When once just isn't enough ...

2004-10-01 19:05 • by Mike Dimmick
You do need to call Guid.NewGuid(), otherwise you're using an uninitialised, blank GUID structure, i.e. {00000000-0000-0000-0000-000000000000}. Because it's a structure, you can't define an explicit parameterless constructor.

Digging around with Reflector and the Shared Source CLI code, you can see that NewGuid calls a private constructor which takes a boolean. The constructor zeros the fields, then, if the argument is true, it calls CompleteGuid. This method is an internal call, i.e. implemented in the virtual machine itself. Searching ecall.cpp shows that the implementation is called GuidNative::CompleteGuid, which lives in comutilnative.cpp. This function does a bit of frame setup then calls CoCreateGuid. I presume that this was done for consistency with how the OS generates GUIDs (using the MAC address for NT 4.0 and earlier, randomly for Windows 2000 and newer).

re: When once just isn't enough ...

2004-10-02 08:09 • by Robert
There is nothing random about a GUID. Its a timestamp, based on your mac address.

re: When once just isn't enough ...

2004-10-03 13:54 • by Fubarer
Is that XML in the comments?

re: When once just isn't enough ...

2004-10-04 01:30 • by Cobus
It's because of developers like this that we're gonna run out of GUIDs one day soon... ;-)

re: When once just isn't enough ...

2004-10-04 08:45 • by Jeff S
>>Is that XML in the comments?

Yes. If you use XML comments with proprer tags in .NET the code can generate documentation automatically.

re: When once just isn't enough ...

2004-10-04 10:08 • by Hassan Voyeau
If the algorithm does include an algorithm identifier then my point (http://thedailywtf.com/archive/2004/10/01/2249.aspx#2267) is null and void. And I guess it would have to, to be Globally Unique. My bad.

re: When once just isn't enough ...

2004-10-04 18:41 • by Anon
Patrick,

<quote>
Scott, sorry but thats nonsense. A million monkeys pounding at a keyboard could duplicate your "unique" GUID, given enough time.
</quote>

Read Scott's comments in context. He was responding to an incorrect statement by Hassan.

re: When once just isn't enough ...

2004-10-04 18:41 • by Anon
Robert,

<quote>
There is nothing random about a GUID. Its a timestamp, based on your mac address.
</quote>

And your evidence is?

re: When once just isn't enough ...

2004-10-06 03:06 • by Cain
This guys is wasting his Guid's - which is perfectly fine by mean because I will sell him some more when he runs out.

re: When once just isn't enough ...

2004-10-06 08:51 • by ML
With 3,4028236692093846346337460743177e+38 combinations it really makes no sense questioning its usability.

Re: When once just isn't enough ...

2011-04-21 14:20 • by TheCPUWizard
For anyone stumbling across this thread, the comments regarting use of MAC address, timestamps, etc. are all obsolete. Due to "privacy concerns" [this is NOT a joke!] GUIDs are now just random numbers wih the same issues of random repeats as any other random number generator. Granted the size [128 bits] minimizes this, but does no eliminate it.

A few years ago, I had a client who was getting occasional database corruption /errors. They had based their entire design on the presumpion that Guids are uniqe. About 3-8 time per week (this system generated over 10K records per second when aggregated across the enterprise) they would in fact get a duplicate.

Re: When once just isn't enough ...

2011-09-12 15:39 • by JayC (unregistered)
360003 in reply to 345057
TheCPUWizard:
For anyone stumbling across this thread, the comments regarting use of MAC address, timestamps, etc. are all obsolete. Due to "privacy concerns" [this is NOT a joke!] GUIDs are now just random numbers wih the same issues of random repeats as any other random number generator. Granted the size [128 bits] minimizes this, but does no eliminate it.

A few years ago, I had a client who was getting occasional database corruption /errors. They had based their entire design on the presumpion that Guids are uniqe. About 3-8 time per week (this system generated over 10K records per second when aggregated across the enterprise) they would in fact get a duplicate.

Sounds like a problem with the random seeder or the GUID generator. If I read http://en.wikipedia.org/wiki/Birthday_paradox right, to have only a 75% chance of collision with a 128 bit key, you need to compare over 3.1 × 10^19 records. That's quite a bit more than 10000*24*60*60 = 8.64*10^8
« PrevPage 1Next »

Add Comment