How likely is it to generate a Repeated Guid?

13

What is the probability of generating a Repeated Guid with Guid.NewGuid() ?

I'm uploading numerous images from my system.

The same will be a MultiTenancy, and will share the same Deploy, from the same folders.

I assign the name of the images with a Guid + extension (jpg, png or gif)

Is there any probability of generating an existing Guid ?

    
asked by anonymous 14.11.2014 / 18:46

3 answers

18
  

Although each generated GUID has no guarantee of being unique, the total number of unique keys (2 128 or ~ 3.4 × 10 38 ) is so large that the probability of the same number being generated twice is very small. For example, considering that the Observable Universe contains 5x10 22 stars, each star could have ~ 6.8 × 10 15 of its own GUIDs.

Source: Wikipedia

According to this answer in the OS there is a 1% chance of collision if it generates 2,600,000,000,000,000,000 GUIDs.

And in this other seems to show you how to create a collision. It is pretty hard. It delays absurdly.

Articles #

excellent Eric series Lippert .

Some people may not understand that GUID is made up of so many distinct information that it makes such an effort to be unique that most other attempts that somebody thinks will be worse, including those quoted on this page. I'm not saying that GUID is the best solution to the actual AP problem, only reinforcing the idea that it is more unique than the cold number indicates.

If Microsoft thinks it's good for it to use the GUID in many of its applications to ensure uniqueness, who am I to say it's no good?

I'll tell you more about this answer .

    
14.11.2014 / 18:52
7

Although the generation of GUIDs is pseudo-random, I think we can consider it completely random for a napkin calculation.

The variable characters are 32, each of which has 16 possible values.

This is more or less (2 4 ) 2 5 . I completely forgot about the high school formulas, so I played ( Math.pow(16, 32) ) on the browser console and gave something close to 3.4x (10 38 ).

I will write an approximation of this in extenso. We have (relatively) a little more than 3,400,000,000,000,000,000,000,000,000,000,000,000 possible combinations.

If you generate two equal GUIDs, enjoy and play a few times in the mega-sena (36,045,979,200 possible combinations, for comparison purposes only).

    
14.11.2014 / 19:00
1

As commented in the chat, and complemented the response of Maniero and Renan.

Using a GUID, it can be difficult to generate a collision, but using a number of that size, 38 houses, can cause problems such as space, memory, search, among others, of course depending on the application these problems do not exist.

My solution, use a library that generates pseudorandom numbers, take a module of 5 decimal places, is enough enough not to become a predictable number, doing this concatenates the right of a sequential number, so its possible problems described above will be harder to come by, plus you'll have the assurance that it will never be repeated.

Example

0001 numero sequencial
58976 numero gerado aleatoriamente

Your 000158976.jpeg file for more than the first part is predictable the second is not.

Look at my working example

link

string g;
    g = Guid.NewGuid().ToString();
    string nome = g.Substring(0, 5);
    nome = x.ToString()+nome;
    Console.WriteLine(nome);
    
14.11.2014 / 19:18