创建唯一的 10 个字母数字字符串
我正在寻找创建一个简单的短期预订系统,并且我想生成确认号,这些确认号是
- 独特的
- 、随机的
- 、短的字母
- 数字,至少比 sha1 返回的 32 个字符长的字符串短得多
。只希望有大约 500 个预订,所以我想象发生碰撞的可能性不大。
我的一个想法是根据日期时间戳和用户名生成 sha1 哈希值,然后将其截断为前 10 个字符。 类似的东西是否足够可靠、独特,足以处理约 500 个预订?
I'm looking to create a simple short-lived reservation system, and I'd like to generate confirmation numbers that are
- unique
- random-looking
- alphanumeric
- short-ish, at least much shorter than 32 character-long strings returned by sha1
I'm only looking to have ~500 reservations, so I don't imagine high likelyhood of collissions.
One idea I had is generate an sha1 hash based on a date-time stamp and username, then truncating it to its first 10 characters. Would something like that be reliably unique enough for the purposes of processing ~500 reservations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
SHA-1 哈希的任何给定位的随机性应该没有差异,所以这是可能的。 另一种方法是使用 XOR 将哈希折叠到自身中,直到获得 60 位的数据,然后使用 Base 64 以获得主要是字母数字的结果。
仅当您希望能够为相同的输入数据重复生成相同的 Id 时,才需要这样做。 否则,如果您生成一次随机 ID,并在此之后保留,请使用 Anders 的建议。 如果发生冲突,只需生成另一个冲突即可。
There should be no difference in the randomness of any given bit of a SHA-1 hash, so that's possible. Another way would be to fold the hash into itself using XOR until you have 60 bits worth of data, then encode it using Base 64 to get a mostly alpha-numeric result.
This is only necessary if you want to be able to generate the same Id repeatedly for the same input data. Otherwise, if a random id that you generate once, and hold onto after that, use Anders' suggestion. If you get a conflict, just generate another one.
您可以使用任何东西,甚至是普通的随机数生成器; 但是,您应该检查预订代码是否已存在。 如果是这种情况,请向字符串(日期+用户)添加字符(“x”),直到获得新的随机/sha1/等。
另一个愚蠢的想法:生成 1000 或 2000 个具有所需属性的唯一随机数,将它们存储在某处,并在用户注册时将它们分配给用户:)
You can use whatever, even a plain random number generator; however, you should check that the reservation code isn't already present. If this is the case, add characters ('x') to the string (date+user) until you get a new random/sha1/etc.
Another stupid idea: generate 1000 or 2000 unique random numbers with the desired properties, store them somewhere, and assign them to the users as they register :)
这是在 Perl 中执行此操作的一种方法:
我不记得 time() 部分有多长,因此您可能需要调整数字以适合您的长度。 如果不需要,您也可以删除该部分。
Here's one way to do it in Perl:
I don't remember how long the time() part is, so you may have to adjust the numbers to fit your length. You can also remove that part if you don't need it.
如果确实只有 500 个,则预先生成其中 20,000 个,放入一个表中,然后在需要时获取“下一个未使用的”。
If it's really just 500, then pre-generate 20,000 of them, into a table, then get the "next unused one" when you need it.
关于这个问题的一些好提示:我该怎么办在 C++ 中创建随机字母数字字符串?
我会避免包含“1”、“l”和“O”、“0”和“5”、“S”和“Z”等字符,“2”在您的字符串中,以便客户在需要通过电话读取您的预订代码时更轻松。 该链接中提供的算法应该可以帮助您做到这一点。
Some good tips on this question: How do I create a random alpha-numeric string in C++?
I'd avoid including characters like "1", "l", and "O", "0" and "5", "S", and "Z", "2" in your string, to make it easier for customers when they need to read your reservation code over the phone. The algorithm presented at that link should help you do this.
使用指南? 16 个字符,但如果你真的不关心碰撞,你可以只选择前 n 个字符。
use a guid? 16 characters, though if you really don't care about collision, you could just choose the first n characters.
在 C# 中,您可以使用 http://www.dotnetfunda.com/forums/thread1357-how-do-generate-unique-alpha-numeric-random-number-in-aspnet.aspx (超级简单的方法,他们说)
In C# you can use http://www.dotnetfunda.com/forums/thread1357-how-do-generate-unique-alpha-numeric-random-number-in-aspnet.aspx (the super easy way, they say)