Can any one please suggest me a unique random string generator in python.
I have used 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 which uses /dev/random. 。 Is its entropy ?? ? 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 ?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.