safe enough 8-character short unique random string

前端 未结 7 1345
甜味超标
甜味超标 2021-02-01 01:34

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?



        
7条回答
  •  礼貌的吻别
    2021-02-01 02:09

    Which method has less collisions, is faster and easier to read?

    TLDR

    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]
    

    Test collisions

    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:

    • collisions: 17 - 11632
    • time (seconds): 37 - 63

提交回复
热议问题