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.
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
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
These are largely based on the clock, so much less random but the uniqueness is better
A 64-bit random number from the OS's random number generator rather than a PRNG:
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)
).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.
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.