日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

發(fā)布時(shí)間:2024/10/14 73 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python int64,如何从Python生成唯一的64位整数? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

解決方案

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

總結(jié)

以上是生活随笔為你收集整理的python int64,如何从Python生成唯一的64位整数?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。