How to generate unique 64 bits integers from Python?

后端 未结 4 1824
心在旅途
心在旅途 2020-12-13 06:41

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.

D

4条回答
  •  情深已故
    2020-12-13 07:09

    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)
    

提交回复
热议问题