Can any one please suggest me a unique random string generator in python.
I have used uuid.uuid1() before which is not good since it is showing some irregularities in different operating systems. You need to import uuid ahead of all the packages in a project for issue-less working which is not a good practice.
I have also heard about os.urandom(n) which uses /dev/random. Is its entropy good enough ?? I am not sure about that.
I can't afford a collision of strings in my process.
Is there any other method can help me in this ?
1 Answer
What about the random and string modules?
>>> import string
>>> import random
>>> ''.join(random.sample(string.letters*5,5))
'QcQxx'
Explanation:
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
I multiply it by 5 so it can be even more random. The higher the number, the more duplicates you are more likely to obtain.
random.sample(k, n) returns n random elements from string.letters, but returns it as a list.
''.join() is called to join the list into a simple string.
4 Comments
Agos
I made an error upvoting, sorry. This solution won't repeat letters, and as such is not really random.
PaulMcG
I've used
''.join(random.choice(string.letters) for i in range(DESIRED_LENGTH)), which addresses @Agos's concern. Still no guarantees against duplicates, but a sufficiently long string should be sufficient for use around the house.Livius
You can also use
itertools.combinations_with_replacement() and then use the random.choice() to select from that. Or if you want to guarantee that a string never occurs twice (but at the cost of a bit of memory), you can do convert the generator from combinations_with_replacement() to a list, then use random.shuffle() to randomize the sequence and pop() off the strings one by one.Alfe
The solution in the answer does not scale. Consider the case you want a 1GB long random letter string. You'd create a 52GB long string for that first. Also, by using
random.sample in this way, the first letters influence the following. For a combination of two letters, the chance for a double (two equal letters) should be ¹/₅₂; using this technique it is only ¹/₁₀₃.
urandomdelivers an endless stream of bytes. If your sample is just one byte long, then the chance to have a collision reaches 1 already at the 257th sample.