1

I was working with Guid.NewGuid() in C# , .NET 4.0 . And I've found interesting fact: every Guid I've generated has '4' at 13 position.

da1471ac-11f7- 4 fb7-a7fa-927fffe8a97c
c90058aa-5d7f- 4 bb5-b3a9-c1db197cf3b1
fa68ec75-8cd2- 4 c24-92f8-41dbbdd428a0
d4efd455-e892- 4 3ef-b7bf-9462c5dc4de4
e0a001a0-8969- 4 092-b7a2-e410ed2b351a
30ae98b9-48ae- 4 25d-b6e7-e091502d6ce2
6a95de82-67ff- 4 4c9-9f7b-e37a80462cf7
66768e46-6d60- 4 2b4-b473-2f6f8bc1559a

I've tried it on several machines and have the same result. Can anybody try it too or explain it?

Simple code for checking:

static void Main(string[] args)
{
     bool ok = false;
     for (int i = 0; i < 10000; i++)
     {
         var guid = Guid.NewGuid();
         if (guid.ToString()[14] != '4')
             ok = true;
         Console.WriteLine(guid);
     }
     Console.WriteLine(ok ? "No bug!" : "4bug founded!");
}
Omar
  • 16,329
  • 10
  • 48
  • 66
VorobeY1326
  • 782
  • 10
  • 25
  • 1
    http://blogs.msdn.com/b/ericlippert/archive/2012/04/30/guid-guide-part-two.aspx – L.B Jul 23 '12 at 18:21
  • 1
    What exactly is the problem? They are all unique. – Mike Perrenoud Jul 23 '12 at 18:21
  • 8
    A guid is *not* a random number. Random numbers are not unique. – Hans Passant Jul 23 '12 at 18:22
  • What makes you think that it's a but.. it would be a but if you were able to positively prove that you can return the exact same GUID twice.. hence the word GUID Global Unique Identifier ...do a google search on GUIDs in C# or look at MSDN for more clarification – MethodMan Jul 23 '12 at 18:22
  • I think @VorobeY1326 has fallen to the common misconception that a Guid is random. – Joseph Yaduvanshi Jul 23 '12 at 18:22
  • See this answer http://stackoverflow.com/questions/1668353/how-can-i-generate-a-cryptographically-secure-pseudorandom-number-in-c for generating a cryptographically secure random number – JP Alioto Jul 23 '12 at 18:24
  • Well, it's a common misconception because it's mostly true; almost all of a V4 GUID (121 of 128 bits) is composed of randomly-generated data by definition. However, the PRNG behind a Windows GUID as used in .NET is not cryptographically strong, being mostly random is merely an implementation detail of a particular type of GUID which may not apply to GUIDs in another system like SQL Server, and finally even a "random" GUID is not 100% random, and so when using them as such you introduce bias. – KeithS Jul 23 '12 at 18:32
  • 1
    In general, when you think you might have found a bug in a language/operating system/established framework/etc. assume that you are wrong. While it is indeed possible, it's far more likely that you just don't understand why you are supposed to be seeing what you're seeing. You will come out looking better if you phrase your question accordingly (even if it's actually a bug). In this case, you should ask something like, "Why do I keep getting "4" in the same digit every time I generate a GUID?" – Servy Jul 23 '12 at 18:46
  • Here is a good example of Guid generation algorithm: http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx – RBZ Jul 23 '12 at 18:22

4 Answers4

12

This is indeed the case and not a bug, it's a feature. It specifies what method was used to generate the GUID. In the case of 4, those GUIDs are generated randomly.

Eric Lippert has a fantastic series on this topic:

Part One Part Two Part Three

Femaref
  • 60,705
  • 7
  • 138
  • 176
5

It's in the algorythm specification. V4 GUIDs must have a "4" on that position. If you're interested in the details, give this a shot: http://en.wikipedia.org/wiki/Globally_unique_identifier#Algorithm

Máté Gelei
  • 869
  • 6
  • 8
0

This really is of no consequence. Guids are generated from a number of different components in order to help maximize the chances that they are unique. For example, I believe one component is an ID on your network card. Part is the date. And so on.

I can't tell you off the top of my head where the 4 came from, but this isn't at all a surprise and is definitely not a bug as you seem to be suggestion.

You can learn more about the algorithm used to generate this number at http://en.wikipedia.org/wiki/Globally_unique_identifier.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 2
    That position is the GUID Version identifier, and a 4 indicates that pretty much all other data in the GUID was randomly generated. This is more secure for most purposes than a V1 GUID which uses the machine's MAC address and a datestamp (which is practically guaranteed to be universally unique but is a HUGE privacy hole). – KeithS Jul 23 '12 at 18:27
0

This is expected behavior, not a bug. This position in the GUID identifies the basic generating algorithm that produced the GUID. There are a few different types; a V4 GUID is, with the exception of seven bits in defined locations of the GUID that identify the version and type variant, composed of random data. Other GUIDs use machine NICs and timestamp values.

KeithS
  • 70,210
  • 21
  • 112
  • 164