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


i have been looking @ similar questions regarding how generate random numbers in python. example: similar question - not have problem randomfunction returns same values every time.

my random generator works fine, problem returns same value when calling function at, think, same second undesireable.

my code looks this

def getrandomid():     token = ''     letters = "abcdefghiklmnopqrstuvwwxyzabcdefghijklmnopqrstuvwxyz1234567890"     in range(1,36):         token = token + random.choice(letters)     return token 

as mentioned function returns different values when being called @ on different times returns same value when calling function @ same time. how avoid problem?

i use function in back-end-server generate unique ids users in front-end insert in database cannot control time intervals when happens. must have random tokens map users in database able insert them correctly queuenumbers in database.

you possibly improve matters using random.systemrandom() follows:

import random  sys_random = random.systemrandom()  def getrandomid():     token = ''     letters = "abcdefghiklmnopqrstuvwwxyzabcdefghijklmnopqrstuvwxyz1234567890"     in range(1,36):         token = token + sys_random.choice(letters)     return token  print getrandomid() 

this attempts use os.urandom() function generates random numbers sources provided operating system.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -