Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python generate random: uuid v. md5 v. random which is most efficient or *inefficient*

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?

like image 923
Williams Avatar asked Dec 13 '14 05:12

Williams


People also ask

How do you generate a random UUID 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.

How fast is UUID generation?

About 20 microseconds per UUID.

What is the difference between uuid1 and uuid4?

uuid1 is "more unique", while uuid4 is more anonymous. So basically use uuid1 unless you have a reason not to.

Is UUID uuid4 unique?

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.


1 Answers

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
like image 97
U2EF1 Avatar answered Oct 27 '22 22:10

U2EF1