1

http://msdn.microsoft.com/en-us/library/system.guid.aspx

How does it work? How does it guarantee unique ID? Does it only rely on probability or is there another trick? Can I always trust it to give unique id or what are cases where I should avoid guid?

Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
  • 2
    What EXACTLY cant you understand by reading documents returned when searching for "GUID" on google.com, wikipedia.org or stackoverflow.com? – Euphoric Nov 30 '10 at 07:46
  • 2
    the answers to the questions in the description – Louis Rhys Nov 30 '10 at 07:58
  • and I think since SO intends to become a reference website, the fact that the info can be found in google or wikipedia does not mean that it shouldn't be asked here – Louis Rhys Nov 30 '10 at 09:46

5 Answers5

7

Here's a fun little program:

class Program
{
    static void Main(string[] args)
    {
        var guids = new HashSet<string>();
        var sw = Stopwatch.StartNew();

        while(true)
        {
            var guid = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
            if (guids.Contains(guid)) break;
            guids.Add(guid);
        }

        Console.WriteLine("Duplicate found in {0} ms, after {1} items.", sw.ElapsedMilliseconds, guids.Count);
        Console.ReadLine();
    }
}

I bet you'll run out of memory before it finds a dupe ;)

Edit: Even when using only half a Guid.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • When using 12/32 characters, I get "Duplicate found in 11107 ms, after 7860493 items." Each character you add makes it something like 16 times less likely. – mpen Nov 30 '10 at 08:52
  • 4
    Don't use "half of the GUID" ever http://stackoverflow.com/questions/1801961/using-parts-of-guid-as-id Only the GUID as a whole is designed to be unique. – sharptooth Nov 30 '10 at 09:02
3

A Guid doesn't guarantee a unique ID, it just has a very low probability of generating a duplicate one.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
  • Not so - Guids are actually guaranteed unique, using a combination of mac address & time stamp: https://blogs.msdn.microsoft.com/oldnewthing/20080627-00/?p=21823 – niico Jul 25 '16 at 13:11
  • @niico There are several ways to generate guids. Mac address+timestamp is just one of the ways and may have privacy concerns (because of the mac address). Nowadays plain random is also used. Or consecutive numbers from a random start. – Hans Kesting Jul 26 '16 at 07:29
  • OK fair point - so its also wrong to say it doesn't guarantee a unique ID - correctly it depends how they are being generated. Does SQL Server not do this Mac address + date thing out of the box though? – niico Aug 07 '16 at 10:28
1

First of all, the Guid structure does not guarantee anything by itself you can create many guids yourself by an explicit constructor: var myGuid = new Guid(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }); That way you can create how many identical Guids you like.

But I suppose you mean Guids created with the static method Guid.NewGuid(). In the remarks section it is noted that the probability that the Guid is all zeros or equal to another Guid is low.

You can also read the RFC that describes how the randomness in a Guid is created.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • Just a quick correction, in the documentation remark section, it says that "The returned Guid is guaranteed to not equal Guid.Empty." so it is *guaranteed* to not be all zeros while yes, there is a *small* probability that the new GUID is duplicated. – Gaboik1 Jan 17 '19 at 20:40
1

It uses a special algorithm designed to produce unique IDs. Yes, they might collide, but that's highly unlikely. Also see this closely related question.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

The .NET Guid.NewGuid() actually calls the function CoCreateGuid found in ole32.dll which is part of Windows "core".

The CoCreateGuid is calling the function UuidCreate which creates such thing, that is actually the "low level" of Guid.

From what I've read and understand (not official answer) it's generated using combination of MAC address of the machine and the current system time (amount of milliseconds that passed since 1/1/1970) and probably also random number but I'm not sure about this.

Avoid using GUID in the cases described under "Security Considerations" section in the link posted by Albin.

All your other questions has been answered in the other answers, no point to repeat it.:)

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208