开发者

Is it possible to create identical guids

开发者 https://www.devze.com 2023-02-13 16:38 出处:网络
Is it possible to create ide开发者_Python百科ntical guids in one application Guid id = Guid.NewGuid();

Is it possible to create ide开发者_Python百科ntical guids in one application

Guid id = Guid.NewGuid();


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 ;)


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");


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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消