如何从GUID 8个字节的唯一ID? [英] How to generate 8 bytes unique id from GUID?

查看:1287
本文介绍了如何从GUID 8个字节的唯一ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试我们的C#应用​​程序(而不是全球性的,只为一个会议)为我们的活动中使用,只要唯一的ID。你知道下面会产生一个独特的长ID?

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?

public long GenerateId()
{
 byte[] buffer = Guid.NewGuid().ToByteArray();
 return BitConverter.ToInt64(buffer, 0);
}

为什么不直接使用GUID?我们认为8个字节长的不够好。

Why we not use GUID directly? We think 8 bytes long is good enough.

推荐答案

没有,也不会。由于强调了很多次Raymond Chen的博客,该GUID被设计成独特的作为一个整体,如果你切出只是一块它时(例如,​​只有64个字节出的128),它就会失去它的(伪)的独特性担保

No, it won't. As highlighted many times on Raymond Chen's blog, the GUID is designed to be unique as a whole, if you cut out just a piece of it (e.g. taking only 64 bytes out of its 128) it will lose its (pseudo-)uniqueness guarantees.

这里的是:

需要一个客户生成一个8字节的独特的价值,他们最初的想法是生成的GUID并扔掉下半年保持第一八个字节。他们想知道这是否是一个好主意。

A customer needed to generate an 8-byte unique value, and their initial idea was to generate a GUID and throw away the second half, keeping the first eight bytes. They wanted to know if this was a good idea.

没有,这不是一个好主意。    (...)   一旦你看到它是如何的所有作品,很显然,你不能只是扔掉的GUID的一部分,因为所有的部件(当然,除了固定部分)携手建立的唯一性。如果你把所有的三部分离,算法分崩离析。特别是,仅保留了前八个字节(64位)为您提供了时间戳和四个不变位;换句话说,你已经是一个时间戳,而不是一个GUID。

No, it's not a good idea. (...) Once you see how it all works, it's clear that you can't just throw away part of the GUID since all the parts (well, except for the fixed parts) work together to establish the uniqueness. If you take any of the three parts away, the algorithm falls apart. In particular, keeping just the first eight bytes (64 bits) gives you the timestamp and four constant bits; in other words, all you have is a timestamp, not a GUID.

由于它只是一个时间标记,你可以有冲突。如果两个计算机产生这些截断的guid在同一时间中的一个时,就会产生相同的结果。或者,如果系统时钟变为落后的时候,由于时钟复位,你会开始再生,你已经产生了第一次是那个时候的GUID。

Since it's just a timestamp, you can have collisions. If two computers generate one of these "truncated GUIDs" at the same time, they will generate the same result. Or if the system clock goes backward in time due to a clock reset, you'll start regenerating GUIDs that you had generated the first time it was that time.


我尝试我们的C#应用​​程序(而不是全球性的,只有一个会话)。我们的活动中使用,只要唯一的ID。你知道下面会生成一个唯一长ID?

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 the following will generate an unique long id?

你为什么不只是使用一个计数器?

Why don't you just use a counter?

这篇关于如何从GUID 8个字节的唯一ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆