safe enough 8-character short unique random string

前端 未结 7 1339
甜味超标
甜味超标 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:18

    You can try this

    import random
    uid_chars = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
                 'v', 'w', 'x', 'y', 'z','1','2','3','4','5','6','7','8','9','0')
    uid_length=8
    def short_uid():
        count=len(uid_chars)-1
        c=''
        for i in range(0,uid_length):
            c+=uid_chars[random.randint(0,count)]
        return c
    

    eg:

    print short_uid()
    nogbomcv
    

提交回复
热议问题