Skip to content

Instantly share code, notes, and snippets.

@reywood
Created November 18, 2022 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reywood/f2c176e4ab0565f7a25ccc0c1b8c0f39 to your computer and use it in GitHub Desktop.
Save reywood/f2c176e4ab0565f7a25ccc0c1b8c0f39 to your computer and use it in GitHub Desktop.
Generate a random 64 bit integer in python
from uuid import uuid4
_8_bit_mask = 0b11111111
_24_bit_mask = 0b11111111_11111111_11111111
def generate_random_64_bit_number():
u = uuid4()
return (
((u.node & _24_bit_mask) << 40)
+ ((u.clock_seq_hi_variant & _8_bit_mask) << 32)
+ ((u.clock_seq_low & _8_bit_mask) << 24)
+ ((u.time_hi_version & _8_bit_mask) << 16)
+ ((u.time_mid & _8_bit_mask) << 8)
+ u.time_low
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment