I created a console application which just generated random GUIDs, but it came to my attention that it keeps having a 4
at the same location... Why is that?
Here's my code:
Sub Main()
Dim generatedGuids = New List(Of String)
Dim duplicateGenerated As Boolean = False
Dim index As ULong = 0
While Not duplicateGenerated
Dim generatedGuid As String = Guid.NewGuid.ToString
generatedGuids.Add(generatedGuid)
duplicateGenerated = generatedGuids.Count <> generatedGuids.Distinct.Count
index += 1
Console.WriteLine(index & " - " & generatedGuid)
End While
Console.WriteLine("FOUND A DUPLICATE")
End Sub
(It's in VB.Net because I just took some online courses, and was playing around with it.)
Here's a screenshot:
As you can see, each generated GUID has a 4
at the exact same spot... Does anyone have an idea why?
Not all 128 bits of a GUID are random.
This character represents the the UUID version (version 4 in your case), and the four bits of it are not supposed to be random.
There is another one :
The next first character after the next hyphen is not completely random as well, a few bits of it are determined and are actually coding the variant of the UUID version.
Notice that in your run, all the values of this last character are greater than or equal to 8
, and less than c
which means the hexadecimal value has always the first bits at 10 : 10xx
, and it means you are using UUID version 4, variant 1.
see https://en.wikipedia.org/wiki/Universally_unique_identifier for more info.
And... that's all for the determined bits, so don't worry, your GUIDs are still unique!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With