What are the chances to get a Guid.NewGuid () duplicate? [duplicate]

▼魔方 西西 提交于 2019-11-27 02:30:29

问题


Possible Duplicate:
Is a GUID unique 100% of the time?
Simple proof that GUID is not unique

In MSDN you can read:

The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low.

Say that you have an method that will create a file every second, and you use the method Guid.NewGuid() for filename, is it possible to get the same Guid then? Or will the local computer keep track in some way? How low is the chance ?


回答1:


You would never run out of guids. The likelihood of duplicating them is VERY low:

http://betterexplained.com/articles/the-quick-guide-to-guids/




回答2:


The chances of getting two identical guids are astronomically slim even if you are generating guids as fast as you can. (Generating, say, thousands of guids per second for the sole purpose of finding a duplicate.)

Of course, if you want my opinion, I do believe that there will be a time, in a couple of thousand years from now, when we will be colonizing the galaxy, our population will be in the trillions, the number of individual computers embedded everywhere will number in the gazillions, and every single one of those computers will be generating GUIDs at a rate which is unthinkable today, when we will start running into trouble with duplicate guids popping up every once in a while in distant areas of the galaxy, and then it will be like 640k of memory all over again, DLL hell all over again, two-digit-year millenium bug all over again, all of them combined.

The thing with GUIDs is that we don't want them to be huge, because then they would be wasteful, so someone had to come up with a number of bits that is small enough to not be too wasteful and yet large enough to give a reasonable guarantee against collisions. So, it is a technological compromise. In our century 128 bits seem to be a good compromise, but with almost mathematical certainty there will be another century when this compromise will not be so good anymore.




回答3:


There is always some miniscule chance of duplicates, but Globally Unique Identifiers are meant to be just that: globally unique ...not system-wide unique, but as in the Planet Earth unique.

I speculate that, theoretically, you have a better chance of duplicating a UUID across multiple systems, than on a single system. While the OS isn't going to store each GUID that it generates, it probably uses some time based seed data to avoid collisions in itself. Of course this is dependent on the implementation.

Oh, and chances...well there are 3.4 x 10^38 available, Wikipedia says you're more likely to get hit by a meteorite.

I'll also offer an alternative method, the Path.GetTempFileName() method may be worth looking into, because it has protection against collision...though it can only create 65,535 unique file names before throwing an exception if previous files aren't deleted.

Other than that, it's not very difficult to do:

string path;

do
{
    path = Guid.NewGuid().ToString(); // Format as needed

} while (File.Exists(path));



回答4:


Please read the following question for info about collisions: Are GUID collisions possible?

Regarding the "one guid per second", generated guids usually take time into account, so on the same computer, 0 chance to generate the same guid unless you change the settings of the internal clock

edit: apparently the wikipedia page about guids says that time is not mandatory when building a guid so i guess it depends on the algorithm used. Since we're talking C# (on windows?) it's in fact a UUID which does include timing in some versions.




回答5:


You will definitely run out of disk space or jam the filesystem before GUIDs collide. Just handle errors when creating files gracefully and you'll be fine.




回答6:


You should not worry about getting duplicate GUID when you are creating it every second. You can refer to http://en.wikipedia.org/wiki/Universally_unique_identifier and read the probability of duplicate GUID.



来源:https://stackoverflow.com/questions/8642858/what-are-the-chances-to-get-a-guid-newguid-duplicate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!