如何生成的8位唯一号码? [英] How to Generate Unique Number of 8 digits?
问题描述
我使用此代码生成一个8位数的唯一编号。
I am using this code to generate a 8 digit unique number.
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToUInt32(buffer, 8).ToString();
这段代码真正生成一个唯一的编号,还是会再次重复同样的号码吗?
Does this code really generate a unique number or might it repeat the same number again?
推荐答案
一个GUID不只是一个随机数;它的组成部分的。一些段,如果在相同的计算机上生成的GUID将不会发生任何变化。通过使用你正在打破的GUID的结构和最有可能打破所生成的数字的唯一性的原始的128位的唯一的64位。
A GUID is not just a random number; it's composed of segments. Some of the segments will not change at all if the guid is generated on the same computer. By using only 64-bits of the original 128-bits you are breaking the structure of the guid and most likely breaking the uniqueness of the generated number.
这问题对GUID的独特性更多信息,请查看此链接以及更多的信息,为什么它的坏只使用一个,如果你需要一个唯一的编号GUID。
This question has more info on uniqueness of guids, check this link as well for more info on why it's bad to use only part of a guid if you need a unique number.
如果你需要重复限制降到最低,增量计数器会给你你所需要的。如果您的应用程序使用多个线程或进程,计数器可能很难(甚至不可能)正确地执行。
If you need to limit duplication to an absolute minimum, an incremental counter will give you what you need. If your application is using multiple threads or processes, a counter may be hard (or even impossible) to implement correctly.
这是GUID的被设计为,为的境界可以在多台机器唯一的。因此,如果跨机器的独特性是一个要求,你应该使用的GUID。整个GUID。
This is the realm that guids were designed for, to be unique across multiple machines. So if uniqueness across machines is a requirement you should use guids. The whole guid.
这篇关于如何生成的8位唯一号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!