I am trying to compute 8-character short unique random filenames for, let\'s say, thousands of files without probable name collision. Is this method safe enough?
The random.choice()
is a bit faster, has about 3 orders of magnitude less collisions but is IMO slightly harder to read.
import string
import uuid
import random
def random_choice():
alphabet = string.ascii_lowercase + string.digits
return ''.join(random.choices(alphabet, k=8))
def truncated_uuid4():
return str(uuid.uuid4())[:8]
def test_collisions(fun):
out = set()
count = 0
for _ in range(1000000):
new = fun()
if new in out:
count += 1
else:
out.add(new)
print(count)
test_collisions(random_choice)
test_collisions(truncated_uuid4)
Results on a single run with 10 million draws of 8-char uuids from the set abcdefghijklmnopqrstuvwxyz0123456789
. Random choice vs truncated uuid4: