How to generate 8 bytes unique id from GUID?

后端 未结 10 1658
长情又很酷
长情又很酷 2020-12-04 01:21

I try to use long as unique id within our C# application (not global, and only for one session) for our events. Do you know if the following will generate an unique long id?

10条回答
  •  离开以前
    2020-12-04 02:19

    Like a few others have said, only taking part of the guid is a good way to ruin its uniqueness. Try something like this:

    var bytes = new byte[8];
    using (var rng = new RNGCryptoServiceProvider())
    {
        rng.GetBytes(bytes);
    }
    
    Console.WriteLine(BitConverter.ToInt64(bytes, 0));
    

提交回复
热议问题