I want to generate a random short hex string (say 8-digit or 16-digit).
There are tons of options to do this eg, from the top of my head:
uuid.uuid4().hex[:8]
md5().hexdigest()[:8]
"{0:08x}".format(int(random.random()*1000000000))[:8]
What I'm wondering is if there's any reason why any of these methods would be more efficient than any others, or inversely if one would be especially evilly inefficient?
Anyone have any good oil on this?
Any suggestions for the cheapest way of doing this operation in python?
You can create duplicates UUIDs by creating more 16384 uuid1 in less than 100ns. Don't use uuid1 when you don't want to make the MAC address of your machine visible. UUID4() uses the cryptographically secure random number generator to generate UUID. uuid4() generates a random UUID.
About 20 microseconds per UUID.
uuid1 is "more unique", while uuid4 is more anonymous. So basically use uuid1 unless you have a reason not to.
If all you want is a unique ID, you should probably call uuid1() or uuid4() . Note that uuid1() may compromise privacy since it creates a UUID containing the computer's network address. uuid4() creates a random UUID. New in version 3.7.
Try them:
> %timeit uuid.uuid4().hex[:8]
100000 loops, best of 3: 7.46 µs per loop
> %timeit "{0:08x}".format(random.randint(0, 0xffffffff))
100000 loops, best of 3: 2.05 µs per loop
> %timeit binascii.hexlify(os.urandom(4))
1000000 loops, best of 3: 1.74 µs per loop
It's notable here that bits from random
are not suitable for cryptographic purposes, so while it is likely to be the fastest that may still not be what you want.
If you're looking for crazy efficient, just get a whole bunch of random data beforehand :)
> randomdata = binascii.hexlify(os.urandom(1024))
> %timeit randomdata[64:72]
10000000 loops, best of 3: 101 ns per loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With