13

Is it possible to create identical guids in one application

Guid id = Guid.NewGuid();
Zlobaton
  • 664
  • 1
  • 9
  • 21
  • [See this question/answer](http://stackoverflow.com/questions/3004151/could-this-cause-multiple-identical-guids). – Uwe Keim Feb 28 '11 at 10:04
  • It can helps you.. http://stackoverflow.com/questions/467271/how-random-is-system-guid-newguid http://stackoverflow.com/questions/300786/duplicate-returned-by-guid-newguid – Yuriy Feb 28 '11 at 10:04

6 Answers6

16

Technically, yes. A created Guid looks for example like this:

26de36b7-76f5-4f17-8f9d-44eb429f151b

That means 32 chars that can be a letter (26 possibilities) or a digit (10 possibilities)

That means 36 posibilities per position for a total of 36^32 that is approx. 60 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000.

That means that if you create 2 000 000 000 000 000 000 000 000 000 000 000 000 000 Guids every millesecond (which is impossible), you will on average get the same guid created twice one time, and all the other guids will be unique.

So in practice. No ;)

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • 1
    Aren't GUIDs represented as hex? If so, you don't have 26 letters to choose from, you have 6. – Craig W. Apr 05 '13 at 20:59
  • @CraigW. - That is an excellent point :) That means the math is a bit of, but the theory is still the same: With 16 possibilities it means that the total number of possible GUIDs are 3,4e38 instead of 6,33e49. Still more than high enough to avoid collisions. – Øyvind Bråthen Apr 08 '13 at 10:02
12

If you are asking if the risk of Guid.NewGuid() creating duplicate guids is high, then the answer is no. This is taken from Wikipedia:

The value of a GUID is represented as a 32-character hexadecimal string, such as {21EC2020-3AEA-1069-A2DD-08002B30309D}, and is usually stored as a 128-bit integer. The total number of unique keys is 2128 or 3.4×1038 — roughly 2 trillion per cubic millimeter of the entire volume of the Earth. This number is so large that the probability of the same number being generated twice is extremely small.

If you are asking us how to create two duplicate guids, then this is the answer:

Guid g1 = new Guid("21EC2020-3AEA-1069-A2DD-08002B30309D");
Guid g2 = new Guid("21EC2020-3AEA-1069-A2DD-08002B30309D");
Andreas Vendel
  • 716
  • 6
  • 14
1

Theoreticaly? Yes

Practicaly? You have higher chance of winning lottery 10 times in a row, than creating two same GUIDs, even in single application.

See Simple proof that GUID is not unique

Community
  • 1
  • 1
Euphoric
  • 12,645
  • 1
  • 30
  • 44
  • 1
    Actually, you probably have a better chance of winning the lottery every week for the rest of your life (if the odds arent to horrible) ;) – Øyvind Bråthen Feb 28 '11 at 10:11
0

Guid.NewGuid() will always create a unique Guid, worldwide, not only inside a single application.

Olaf
  • 10,049
  • 8
  • 38
  • 54
  • 1
    Not any more, please check the specification. Windows, at least, uses V4 of the GUID specification, and the unique part has been reduced to a single system. Unlikely to generate duplicate keys across systems, but still possible. – Lasse V. Karlsen Feb 28 '11 at 10:14
-1

The U in GUID stands for Unique. ;-) So it shouldn't be possible.

Keith
  • 42,110
  • 11
  • 57
  • 76
-2

I've actually just had this happen. I had a database table containing 7 items. From my program, I added a new instance, using Guid.NewGuid() for its ID. I got a DbUpdateException telling me the ID was identical to an existing one. Tried it again, it works fine.

jonsca
  • 10,218
  • 26
  • 54
  • 62
  • 1
    the odds are so tiny that you would see a clash that I would look for a bug / threading issue rather than suspect the guid algorithm itself - see [this article on the guid algorithm](http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx). – chris Sep 24 '12 at 10:00