开发者

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

开发者 https://www.devze.com 2023-03-25 01:06 出处:网络
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.

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()
{
   unch开发者_JS百科ecked
   {
      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?


You can just use a random number generator instead, either System.Random or System.Security.Cryptography.RNGCryptoServiceProvider.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消