如何从 Python 生成唯一的 64 位整数?

发布于 2024-09-15 10:14:48 字数 186 浏览 2 评论 0原文

我需要从 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技术交流群

发布评论

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

评论(5

最终幸福 2024-09-22 10:14:48

只需屏蔽 128 位 int

>>> import uuid
>>> uuid.uuid4().int & (1<<64)-1
9518405196747027403L
>>> uuid.uuid4().int & (1<<64)-1
12558137269921983654L

这些或多或少是随机的,因此发生冲突的机会很小

也许 uuid1 的前 64 位使用起来

>>> uuid.uuid1().int>>64
9392468011745350111L
>>> uuid.uuid1().int>>64
9407757923520418271L
>>> uuid.uuid1().int>>64
9418928317413528031L

更安全这些主要基于时钟,因此随机性要少得多,但唯一性更好

just mask the 128bit int

>>> import uuid
>>> uuid.uuid4().int & (1<<64)-1
9518405196747027403L
>>> uuid.uuid4().int & (1<<64)-1
12558137269921983654L

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

>>> uuid.uuid1().int>>64
9392468011745350111L
>>> uuid.uuid1().int>>64
9407757923520418271L
>>> uuid.uuid1().int>>64
9418928317413528031L

These are largely based on the clock, so much less random but the uniqueness is better

深海蓝天 2024-09-22 10:14:48

64 位唯一

计数有什么问题?一个简单的计数器将创造独特的价值。这是最简单的,很容易确保您不会重复某个值。

或者,如果计数不够好,请尝试这个。

>>> import random
>>> random.getrandbits(64)
5316191164430650570L

根据您播种和使用随机数生成器的方式,它应该是唯一的。

当然,您可能会错误地执行此操作并获得重复的随机数序列。必须非常小心地处理启动和停止程序的种子。

64 bits unique

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.

>>> import random
>>> random.getrandbits(64)
5316191164430650570L

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.

逆光飞翔i 2024-09-22 10:14:48

来自操作系统随机数生成器的 64 位随机数,而不是 PRNG:

>>> from struct import unpack; from os import urandom
>>> unpack("!Q", urandom(8))[0]
12494068718269657783L

A 64-bit random number from the OS's random number generator rather than a PRNG:

>>> from struct import unpack; from os import urandom
>>> unpack("!Q", urandom(8))[0]
12494068718269657783L
彩扇题诗 2024-09-22 10:14:48

您可以使用 uuid4() 生成单个随机 128 位整数 UUID。我们必须将每个 128 位整数通过 64 位生成(即 128 - (128 - 64))。

from uuid import uuid4

bit_size = 64
sized_unique_id = uuid4().int >> bit_size
print(sized_unique_id)

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)).

from uuid import uuid4

bit_size = 64
sized_unique_id = uuid4().int >> bit_size
print(sized_unique_id)
¢蛋碎的人ぎ生 2024-09-22 10:14:48

为什么不试试这个呢?

import uuid
  
id = uuid.uuid1()
  
# Representations of uuid1()

print (repr(id.bytes)) # k\x10\xa1n\x02\xe7\x11\xe8\xaeY\x00\x16>\x99\x0b\xdb

print (id.int)         # 142313746482664936587190810281013480411  

print (id.hex)         # 6b10a16e02e711e8ae5900163e990bdb
  

Why not try this?

import uuid
  
id = uuid.uuid1()
  
# Representations of uuid1()

print (repr(id.bytes)) # k\x10\xa1n\x02\xe7\x11\xe8\xaeY\x00\x16>\x99\x0b\xdb

print (id.int)         # 142313746482664936587190810281013480411  

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