公式生成一个唯一的ID?

时间:2022-11-25 12:31:08

I would like to get a few ideas on generating unique id's without using the GUID. Preferably i would like the unique value to be of type int32.

我想在不使用GUID的情况下获得关于生成唯一ID的一些想法。最好我希望唯一值是int32类型。

I'm looking for something that can be used for database primary key as well as being url friendly.

我正在寻找可以用于数据库主键以及网址友好的东西。

Can these considered Unique?

这些可以考虑独特吗?

  1. (int)DateTime.Now.Ticks
  2. (int)DateTime.Now * RandomNumber
  3. (int)DateTime.Now * RandomNumber

Any other ideas?

还有其他想法吗?

Thanks

EDIT: Well i am trying to practise Domain Driven Design and all my entities need to have a ID upon creation to be valid. I could in theory call into the DB to get an auto incremented number but would rather steer clear of this as DB related stuff is getting into the Domain.

编辑:我正在尝试实践领域驱动设计,我的所有实体都需要在创建时有一个ID才有效。理论上我可以调用DB来获得一个自动递增的数字,但是当DB相关的东西进入域时,我宁愿避开这个。

3 个解决方案

#1


It depends on how unique you needed it to be and how many items you need to give IDs to. Your best bet may be assigning them sequentially; if you try to get fancy you'll likely run into the Birthday Paradox (collisions are more likely than you might expect) or (as in your case 1) above) be foreced to limit the rate at which you can issue them.

这取决于您需要它的独特性以及您需要提供ID的项目数量。你最好的选择可能是顺序分配它们;如果你试图想象你可能会遇到生日悖论(碰撞比你想象的更可能)或(如你的情况1)上面的情况下被限制你可以发出它们的速度。

Your 1) above is a little better than the 2) for most cases; it's rate limited--you can't issue more than 1 ID per tick--but not susceptible to the Birthday Paradox. Your 2) is just throwing bits away. Might be slightly better to XOR with the random number, but in any case I don't think the rand is buying you anything, just hiding the problem & making it harder to fix.

在大多数情况下,你的上述1)比2)好一点;它的速率有限 - 每个刻度不能发出超过1个ID - 但不会受到生日悖论的影响。你的2)只是丢掉了一些东西。使用随机数可能稍微好一点XOR,但无论如何我不认为兰特会给你买任何东西,只是隐藏问题并使其更难修复。

#2


Are these considered Globally Unique?

这些被认为是全球独一无二的吗?

1) (int)DateTime.Now.Ticks 2) (int)DateTime.Now * RandomNumber

1)(int)DateTime.Now.Ticks 2)(int)DateTime.Now * RandomNumber

Neither option is globally unique.

这两种选择都不是全球唯一的。

Option 1 - This is only unique if you can guarantee no more than one ID is generated per tick. From your description, it does not sound like this would work.

选项1 - 如果您可以保证每个刻度不会生成多个ID,则这是唯一的。从你的描述来看,这听起来并不合适。

Option 2 - Random numbers are pseudo random, but not guaranteed to be unique. With that already in mind, we can reduce the DateTime portion of this option to a similar problem to option 1.

选项2 - 随机数是伪随机数,但不保证是唯一的。考虑到这一点,我们可以将此选项的DateTime部分减少为与选项1类似的问题。

If you want a globally unique ID that is an int32, one good way would be a synchronous service of some sort that returns sequential IDs. I guess it depends on what your definition of global means. If you had larger than an int32 to work with, and you mean global on a given network, then maybe you could use IP address with a sequence number appended, where the sequence number is generated synchronously across processes.

如果您想要一个int32的全局唯一ID,一种好方法是返回顺序ID的某种同步服务。我想这取决于你对全球意义的定义。如果您使用的int3大于int32,并且您指的是给定网络上的全局,那么您可以使用附加了序列号的IP地址,其中序列号是跨进程同步生成的。

If you have other unique identifiers besides IP address, then that would obviously be a better choice for displaying as part of a URL.

如果你有除IP地址之外的其他唯一标识符,那么这显然是作为URL的一部分显示的更好选择。

#3


You can use the RNGCryptoServiceProvider class, if you are using .NET

如果您使用的是.NET,则可以使用RNGCryptoServiceProvider类

RNGCryptoServiceProvider Class

#1


It depends on how unique you needed it to be and how many items you need to give IDs to. Your best bet may be assigning them sequentially; if you try to get fancy you'll likely run into the Birthday Paradox (collisions are more likely than you might expect) or (as in your case 1) above) be foreced to limit the rate at which you can issue them.

这取决于您需要它的独特性以及您需要提供ID的项目数量。你最好的选择可能是顺序分配它们;如果你试图想象你可能会遇到生日悖论(碰撞比你想象的更可能)或(如你的情况1)上面的情况下被限制你可以发出它们的速度。

Your 1) above is a little better than the 2) for most cases; it's rate limited--you can't issue more than 1 ID per tick--but not susceptible to the Birthday Paradox. Your 2) is just throwing bits away. Might be slightly better to XOR with the random number, but in any case I don't think the rand is buying you anything, just hiding the problem & making it harder to fix.

在大多数情况下,你的上述1)比2)好一点;它的速率有限 - 每个刻度不能发出超过1个ID - 但不会受到生日悖论的影响。你的2)只是丢掉了一些东西。使用随机数可能稍微好一点XOR,但无论如何我不认为兰特会给你买任何东西,只是隐藏问题并使其更难修复。

#2


Are these considered Globally Unique?

这些被认为是全球独一无二的吗?

1) (int)DateTime.Now.Ticks 2) (int)DateTime.Now * RandomNumber

1)(int)DateTime.Now.Ticks 2)(int)DateTime.Now * RandomNumber

Neither option is globally unique.

这两种选择都不是全球唯一的。

Option 1 - This is only unique if you can guarantee no more than one ID is generated per tick. From your description, it does not sound like this would work.

选项1 - 如果您可以保证每个刻度不会生成多个ID,则这是唯一的。从你的描述来看,这听起来并不合适。

Option 2 - Random numbers are pseudo random, but not guaranteed to be unique. With that already in mind, we can reduce the DateTime portion of this option to a similar problem to option 1.

选项2 - 随机数是伪随机数,但不保证是唯一的。考虑到这一点,我们可以将此选项的DateTime部分减少为与选项1类似的问题。

If you want a globally unique ID that is an int32, one good way would be a synchronous service of some sort that returns sequential IDs. I guess it depends on what your definition of global means. If you had larger than an int32 to work with, and you mean global on a given network, then maybe you could use IP address with a sequence number appended, where the sequence number is generated synchronously across processes.

如果您想要一个int32的全局唯一ID,一种好方法是返回顺序ID的某种同步服务。我想这取决于你对全球意义的定义。如果您使用的int3大于int32,并且您指的是给定网络上的全局,那么您可以使用附加了序列号的IP地址,其中序列号是跨进程同步生成的。

If you have other unique identifiers besides IP address, then that would obviously be a better choice for displaying as part of a URL.

如果你有除IP地址之外的其他唯一标识符,那么这显然是作为URL的一部分显示的更好选择。

#3


You can use the RNGCryptoServiceProvider class, if you are using .NET

如果您使用的是.NET,则可以使用RNGCryptoServiceProvider类

RNGCryptoServiceProvider Class