random.choice() returns same value at the same second, how does one avoid it?

后端 未结 3 1773
借酒劲吻你
借酒劲吻你 2021-01-11 10:11

I have been looking at similar questions regarding how to generate random numbers in python. Example: Similar Question - but i do not have the problem that the randomfunctio

相关标签:
3条回答
  • 2021-01-11 10:15

    You could possibly improve matters by using random.SystemRandom() as follows:

    import random
    
    sys_random = random.SystemRandom()
    
    def getRandomID():
        token = ''
        letters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
        for i in range(1, 36):
            token = token + sys_random.choice(letters)
        return token
    
    print(getRandomID())
    

    This attempts to use the os.urandom() function which generates random numbers from sources provided by the operating system. The .choices() function could also be used to return a list of choices in a single call, avoiding the string concatenation:

    import random
    
    sys_random = random.SystemRandom()
    
    def getRandomID():
        letters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
        return ''.join(sys_random.choices(letters, k=35))
    
    print(getRandomID())
    
    0 讨论(0)
  • 2021-01-11 10:19
    def getRandomID(n):
    
        import datetime
        import random
    
        random.seed(datetime.datetime.now())
    
        letters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    
        idList = [ ''.join([random.choice(letters) for j in range(1,36)]) for i in range(n)]
    
        return idList
    

    this script in the 3rd test of 10 million ids again have made them all unique

    changing for loop to list comprehension did speedup quite a bit.

    >>> listt = getRandomID(10000000)
    >>> print(len(listt))
    10000000
    
    >>> setOfIds = set(listt)
    >>> print(len(setOfIds))
    10000000
    

    this script uses permutations with repetition: 62 choose 35, to theoretically total number of ids is quite big it is pow(62,35)

    541638008296341754635824011376225346986572413939634062667808768
    
    0 讨论(0)
  • 2021-01-11 10:38

    Another option would be to update the seed with the previous result to get a pseudorandom sequence. An option would be old_seed XOR result or just the result.

    0 讨论(0)
提交回复
热议问题