如何从Python生成唯一的64位整数? [英] How to generate unique 64 bits integers from Python?

查看:719
本文介绍了如何从Python生成唯一的64位整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Python生成唯一的64位整数.我已经检出了 UUID模块.但是它生成的UUID是128位整数.这样就行不通了.

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.

您知道在Python中生成64位唯一整数的任何方法吗?谢谢.

Do you know of any way to generate 64 bits unique integers within Python? Thanks.

推荐答案

只需屏蔽128位整数

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

也许uuid1的前64位更安全使用

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

这篇关于如何从Python生成唯一的64位整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆