What's the best way to generate random strings of a specific length in Python?

后端 未结 6 981
感情败类
感情败类 2020-12-02 17:05

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.

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 17:35

    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.

    from OpenSSL import rand
    
    b = rand.bytes(7)
    

    BTW, 12 uppercase letters is a little bit more that 56 bits of entropy. You will only to have to read 7 bytes.

提交回复
热议问题