如何从 Python 生成唯一的 64 位整数?
我需要从 Python 生成唯一的 64 位整数。我已经检查了 UUID 模块。但它生成的UUID是128位整数。所以那是行不通的。
你知道在 Python 中生成 64 位唯一整数的方法吗?谢谢。
I need to generate unique 64 bits integers from Python. I've checked out the UUID module. But the UUID it generates are 128 bits integers. So that wouldn't work.
Do you know of any way to generate 64 bits unique integers within Python? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需屏蔽 128 位 int
这些或多或少是随机的,因此发生冲突的机会很小
也许 uuid1 的前 64 位使用起来
更安全这些主要基于时钟,因此随机性要少得多,但唯一性更好
just mask the 128bit int
These are more or less random, so you have a tiny chance of a collision
Perhaps the first 64 bits of uuid1 is safer to use
These are largely based on the clock, so much less random but the uniqueness is better
计数有什么问题?一个简单的计数器将创造独特的价值。这是最简单的,很容易确保您不会重复某个值。
或者,如果计数不够好,请尝试这个。
根据您播种和使用随机数生成器的方式,它应该是唯一的。
当然,您可能会错误地执行此操作并获得重复的随机数序列。必须非常小心地处理启动和停止程序的种子。
What's wrong with counting? A simple counter will create unique values. This is the simplest and it's easy to be sure you won't repeat a value.
Or, if counting isn't good enough, try this.
Depending on how you seed and use your random number generator, that should be unique.
You can -- of course -- do this incorrectly and get a repeating sequence of random numbers. Great care must be taken with how you handle seeds for a program that starts and stops.
来自操作系统随机数生成器的 64 位随机数,而不是 PRNG:
A 64-bit random number from the OS's random number generator rather than a PRNG:
您可以使用
uuid4()
生成单个随机 128 位整数 UUID。我们必须将每个 128 位整数通过 64 位生成(即128 - (128 - 64)
)。You can use
uuid4()
which generates a single random 128-bit integer UUID. We have to 'binary right shift' (>>
) each 128-bit integer generated by 64-bit (i.e.128 - (128 - 64)
).为什么不试试这个呢?
Why not try this?