0

I am developing a Windows Form Application in VB .NET 4.0 and need to generate strings that are unique during the life of the application. I have come across the function "System.Guid.NewGuid.ToString" that seems to solve this problem, but I have read that there still can be "collisions" from time to time.

I could use a global counter variable, but I have had parameter scope issues with this in the past and would like to stay away from it if possible.

Any suggestions would be greatly appreciated.

eniacAvenger
  • 875
  • 12
  • 17
  • 1
    Where do you need them? Collisions from `Guid.NewGuid` are incredibly small. – Tim Schmelter Mar 10 '14 at 14:26
  • For once, wikipedia is a good place to look: [Random_UUID_probability_of_duplicates](http://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates): "... after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%" – Damien_The_Unbeliever Mar 10 '14 at 14:28
  • Just curious, has anyone ever heard of a collision actually happening? – rory.ap Mar 10 '14 at 14:36
  • @roryap yes, but not using v4: http://stackoverflow.com/a/2166066/1070452 – Ňɏssa Pøngjǣrdenlarp Mar 10 '14 at 16:00

1 Answers1

0

The probability of collision is so low that you could generate millions of GUIDs a second and only approach 1% collision chance after several (hundred) years.

This seems like an acceptable solution to me.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • Thank you, this is my first use of GUIDs and just wanted to make sure that my bases were covered. And if anyone challenges this solution, I have good numbers to back it up! :) – eniacAvenger Mar 10 '14 at 14:46
  • If you feel the need for even more entropy you could always append the number of seconds since a specific date to the GUID since you are using a string, but I don't really think it's needed. – Bradley Uffner Mar 10 '14 at 14:49
  • It's funny you should say that because I thought of using a timestamp string as a unique ID but I figured there must be a reason why people use GUIDs over this solution (like the possibility that 2 strings are generated with the same timestamp down to the milliseconds) – eniacAvenger Mar 10 '14 at 14:55
  • A simple timestamp should probably be avoided because typically they are not accurate down to the millisecond even though they have that precision. If you are requesting timestamps in rapid succession you could easily get the same value. The older GUID versions that used timestamps internally actually worked around this same issue by tracking the timestamp and incrementing a counter if the value was too close to the previous timestamp. – Bradley Uffner Mar 10 '14 at 16:53
  • Part of the GUID is a timestamp so there is no need to add a timestamp part to it. – Matt Wilko Mar 10 '14 at 17:07
  • @Matt Not true (at least any more). As of Windows 2000 GUIDs are using the v4 spec, which does not include a timestamp. .NET simply wraps the OS GUID functionality, so you get what the OS gives you. V4 GUIDs are pseudo-randomly generated without containing a timestamp. – Bradley Uffner Mar 10 '14 at 17:10