Search code examples
c#guid

C#, Best 64bit (long) Guid type generator for low collisions. Similar to Guid.NewGuid() but for 64 bits


I want to use a 64bit identifier similar to how Guids are used. Whats a good way of doing this? I want to keep collisions low.

public static unsafe long GetLongGuid()
{
   unchecked
   {
      fixed (byte* ptr = Guid.NewGuid().ToByteArray())
         return *((long*)ptr) ^ *((long*)(ptr + 8));
   }
}

Should I just take the upper or lower bits instead?

Or is there a better native 64bit unique hash function that's good?


Solution

  • You can just use a random number generator instead, such as System.Security.Cryptography.RandomNumberGenerator.

    Exactly why you're using unsafe code here, if not for performance, is very unclear to me.