生成长度为 6 的唯一非连续字母数字字符串

发布于 2025-01-04 10:00:51 字数 417 浏览 2 评论 0原文

我需要生成长度恰好为 6(不少于,不少于)的唯一非连续字母数字字符串。 对于非序列,我的意思是它应该乍一看在视觉上是随机的,而不是来自序列(如果你能在研究几天后找到序列并不重要,它应该只是乍一看是随机的)。 请记住它必须是唯一的。 查找所有已经使用过的号码也是不可行的。

我正在考虑让数据库生成一个唯一的数字,然后应用一些函数将该数字转换为长度恰好为 6 的唯一的非连续字母数字字符串。与哈希算法的工作原理类似,但不会出现可能的冲突。

我发现了这个: http://blog.maxant.co .uk/pebble/2010/02/02/1265138340000.html 但序列在视觉上并不是随机的。

I need to generate unique non-sequential alphanumeric string of exactly length 6 (not less, not more).
With non-sequential I mean it should look visually random at first sight and not coming from a sequence (it doesn't matter if you can find the sequence after some number of days of study, it should just appear random at first sight).
And bear in mind it must be unique.
It's also unfeasible to lookup all the numbers that have been used already.

I was thinking of letting a database generate a unique number and then apply some function to convert that number into a unique non-sequential alphanumeric string of exactly length 6. Similar to how a hashing algorithm works, but then without possible collisions.

I've found this: http://blog.maxant.co.uk/pebble/2010/02/02/1265138340000.html
But the sequence is not visually random.

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

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

发布评论

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

评论(2

墨离汐 2025-01-11 10:00:51

您是否可以将生成的字符串序列注册到某个数据库/文件/某个地方?然后一种方法是对于每个位置(从 1 到 6),随机选择一个字符并形成 6 个字符的字符串。并查看它是否是注册序列之一。如果已经注册,则生成另一个序列。如果未注册,则注册并注册将生成的文件用于您的目的。

Is it possible for you to register the generated String sequence to some database/file/someplace? Then one way to do can be for each position (from 1 to 6), randomly pick a character and form your 6 character string. And see if it is one of the registered sequence or not. If already registered, then generated another sequence. If not registered then register & use the generated one for your purpose.

请恋爱 2025-01-11 10:00:51

通过字母数字,您的目标是 [A-Z0-9][a-zA-Z0-9] 还是 [a-zA-Z0-9 \+\\] 可以接受吗?在后一种情况下,您可以作弊,将您的唯一 ID 与适当大的值进行异或,然后通过 Base64 算法进行全部处理,从而节省工作量。

对于另外两个,请参考简单的伪随机数生成器的书;执行类似 x = ((id + salt)*multiplier) mod pow(alphabet_size,6) 的操作,然后将整数 x “解码”为字母数字字符。当然,选择好的saltmultiplier很重要;理想情况下,后者应该是一个大质数,或者至少是 alphabet_size 的互质。 salt 甚至可以为零,但如果您愿意,可以用于以更具美学价值的输出开始。

只要初始 id 不超过 pow(alphabet_size,6),哈希值就是唯一可逆的。您需要将哈希值转换回整数,然后使用模除算法重新获取 (id + salt),从而获得原始 id

By alphanumeric are you aiming at [A-Z0-9], [a-zA-Z0-9] or would [a-zA-Z0-9\+\\] be acceptable? In the latter case you could cheat, XOR your unique ID with suitably large value and then throw the whole lot through a Base64 algorithm, saving work.

For the other two, take a leaf out of the simple pseudo-random number generator's book; Perform something like x = ((id + salt)*multiplier) mod pow(alphabet_size,6) and then 'decode' the integer x into alphanumeric characters. Of course choosing a good salt and multiplier is important; The latter would ideally be a large prime or at least be coprime to the alphabet_size. The salt could even be zero, but can be used to start at a more aesthetic value for the output if you wish.

As long as the initial id never exceeds pow(alphabet_size,6), the hash will be uniquely reversible. You would need to convert your hash back into an integer and then use a modular division algorithm to re-obtain (id + salt), and thus the original id.

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