生成唯一随机数的有效方法是什么?
我希望为每个用户贡献生成一个随机数作为贡献的标题。
我可以简单地每次使用查询检查数据库并生成一个不等于数据库任何条目的数字。但我认为这效率低下,如果数据库很大,我认为它可能会变得很慢。另外,我必须将数据库的所有数字包含在某个地方来管理“不等于”,在一个数组或类似的东西中,但最终可能会成为一个巨大的数字。
请原谅外行的讲话,我对此很陌生。
有什么建议可以有效地解决这个问题而不会对资源造成太大压力吗?你可以用语言来解释,不需要给我任何脚本,我会弄清楚的。
I am looking to generate a random number for every user contribution as a title of the contribution.
I could simply check the database each time with a query and generate a number which does not equal to any of the entries of the database. But I imagine this as inefficient and it could become slow if the database is big in my opinion. Also I'd have to contain all the numbers of the database somewhere to manage the "not equals to", in an array or something similar but that can end up as a giant one.
Excuse the layman's speech I am new to this.
Any suggestions how this can be solved efficiently without straining the resources too much? You can explain it linguistically and do not have to provide me any scripts, I will figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
uniqid()
。我不确定它的便携性如何。示例:
将输出类似以下内容:
You can use
uniqid()
. I'm not sure how portable it is.Example:
Will output something like:
uniqid() 会给你一个技术上可以重复的随机数。
也许您可以在自动增量字段上应用一个简单的算法? n(n+1)/2 还是什么?
uniqid() will give you a random number that can technically repeat.
Maybe you can apply a simple algorithm on an auto-increment field? n(n+1)/2 or something?