Can I use the first OR last 16 characters of a Guid and it still be unique?

坚强是说给别人听的谎言 提交于 2019-12-11 01:38:39

问题


Objective

Generate a unique 16-character key, to use as a batch name, that doesn't require any type of storage (e.g. a unique counter).

Background

I have a Windows service that runs every 30 seconds. I picks up messages off of an MQ and processes them in a batch. One of the service calls I'm making is requiring a 16-character batch name now. They don't care what the batch name is, it just needs to be unique across all batches.

Question

If I generate a Guid, can I use the first or last 16 characters of that string and it still be unique for my needs?


回答1:


No is the simple answer. Why not use the date/time?

For example, here's some Oracle generated GUIDs

D71CDF38BB3B6026E0430A9A9A286026
D71CDF38BB3C6026E0430A9A9A286026
D71CDF38BB3D6026E0430A9A9A286026
D71CDF38BB3E6026E0430A9A9A286026
D71CDF38BB3F6026E0430A9A9A286026
D71CDF38BB406026E0430A9A9A286026



回答2:


No.

Raymond Chen explains why in great detail in GUIDs are globally unique, but substrings of GUIDs aren't.

However, if you are running this on a single machine, then you don't need your ID to be globally unique - merely locally unique. Therefore, you can drop the MAC address requirement from the GUID algorithm he describes. Also, if you know that only one will be generated every 30 seconds, you can drop the collision part of the algorithm. This pretty much leaves you using the date-time as qujck suggests.



来源:https://stackoverflow.com/questions/15293487/can-i-use-the-first-or-last-16-characters-of-a-guid-and-it-still-be-unique

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!