GUID,30 个字符的随机字符串

发布于 2024-09-10 06:26:39 字数 208 浏览 0 评论 0原文

我需要生成一个长度为 30 个字符的唯一字符串。起初,我打算生成一个 GUID,然后删除前两个字符。

Guid.NewGuid().ToString("N").Substring(2);

删除前两个字符会对“唯一性”产生重大影响吗?这是我应该担心的事情吗?

有没有更好的方法来生成随机 30 个字符的字符串并保证唯一?

I need to generate a unique string that is 30 characters in length. At first, I was going to generate a GUID and just remove the first two characters.

Guid.NewGuid().ToString("N").Substring(2);

Will removing the two first characters have a significant effect on the "uniqueness"? Is it something that I should be worried about?

Is there a better way of generating a random 30 character string that will be guaranteed to be unique?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦途 2024-09-17 06:26:39

从 GUID 中删除两个十六进制字符或等效的 8 位将使其不那么唯一,但 120 位仍然是一个相当好的唯一值。如果您不想每秒生成数百万个 id,那么从时间戳和唯一标识符中删除一些位应该是安全的,而不会有冲突的风险。例如,请参阅 Wikipedia,了解 GUID 结构

另一种解决方案是将 GUID 编码为 Base64 或类似的内容(如果您不限于此)仅限十六进制字符。以 Base64 编码的 128 位会产生长度为 24 的字符串。然后您甚至可以添加另外 6 个随机字符来将字符串填充到 30 个字符,使其更加独特。

Removing two hexadecimal characters or equivalently 8 bits from a GUID will make it less unique but 120 bits still make a quite good unique value. If you don't want to generate millions of ids every second it should be safe to remove some bits from the timestamp and uniquifier without risking a collision. See for example the Wikipedia for the structure of GUIDs.

An alternative solution would be to encode the GUID in Base64 or something like that if you are not constraint to hexadecimal characters only. 128 bits encoded in Base64 yield a string of length 24. Then you can even add another 6 random characters to pad the string to 30 characters making it even more unique.

贵在坚持 2024-09-17 06:26:39

截断 GUID 会失去唯一性。要了解原因,您应该了解 GUID 是如何创建的。它由几个部分组成:

  • 60 位时间戳
  • 48 位计算机标识符
  • 14 位唯一标识符
  • 6 位是固定的

通过丢弃前两个字符,您将丢弃时间戳部分的 8 个最高有效位。 本文很好地解释了这一点以及截断 GUID 的危险。它还解释了如何使用 GUID 中使用的相同技术来创建不是全局唯一的唯一标识符,但在更受限的情况下将是唯一的。

Truncating a GUID loses the uniqueness. To understand why you should understand how a GUID is created. It consists of a few parts:

  • 60 bits of timestamp
  • 48 bits of computer identifier
  • 14 bits of uniquifier
  • 6 bits are fixed

By discarding the first two characters you are discarding the 8 most significant bits of the timestamp part. This article explains it well and the dangers of truncating GUIDs. It also explains how you could use the same technique used in GUIDs to create unique identifiers that are not globally unique but will be unique for more constrained circumstances.

八巷 2024-09-17 06:26:39

正如其他回答者在我之前所说的那样,如果您只是从 GUID 中删除两个字符,那么它将不再是唯一的。

但还有另一种方法:可以通过 ASCII 编码将 GUID 缩短至最多 20 个字符,而不会丢失信息或唯一性。

查看 Jeff Atwood 的这篇博文:
编码恐怖:装备我们的 ASCII 盔甲

As the other answerers said before me, if you just remove two characters from the GUID, then it will not be unique anymore.

But there's another way: it's possible to shorten a GUID to up to 20 characters without losing information or uniqueness by ASCII encoding.

Check out this blog post by Jeff Atwood:
Coding Horror: Equipping our ASCII Armor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文