For a project, I need a method of creating thousands of random strings while keeping collisions low. I'm looking for them to be only 12 characters long and uppercase only. Any suggestions?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
For cryptographically strong pseudo-random bytes you might use the pyOpenSSL wrapper around OpenSSL.
It provides the
bytes
function to gather a pseudo-random sequences of bytes.BTW, 12 uppercase letters is a little bit more that 56 bits of entropy. You will only to have to read 7 bytes.
By
Django
, you can useget_random_string
function indjango.utils.crypto
module.Example:
But if you don't want to have
Django
, here is independent code of it:Code:
This function generates random string of UPPERCASE letters with the specified length,
eg: length = 6, will generate the following random sequence pattern
Could make a generator:
Then just pull from the generator when they're needed... Either using
next(random_gen)
when you need them, or userandom_200 = list(islice(random_gen, 200))
for instance...CODE:
OUTPUT:
5 examples:
EDIT:
If you need only digits, use the
digits
constant instead of theascii_uppercase
one from thestring
module.3 examples: