51

对于一个项目,我需要一种创建数千个随机字符串同时保持低冲突的方法。我正在寻找它们只有 12 个字符长且仅大写。有什么建议么?

4

6 回答 6

124

代码:

from random import choice
from string import ascii_uppercase

print(''.join(choice(ascii_uppercase) for i in range(12)))

输出:

5个例子:

QPUPZVVHUNSN
EFJACZEBYQEB
QBQJJEEOYTZY
EOJUSUEAJEEK
QWRWLIWDTDBD

编辑:

如果您只需要数字,请使用digits常量而不是模块中ascii_uppercase的常量string

3个例子:

229945986931
867348810313
618228923380
于 2013-08-19T16:59:21.637 回答
23

通过Django,您可以使用模块中get_random_string的功能。django.utils.crypto

get_random_string(length=12,
    allowed_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
    Returns a securely generated random string.

    The default length of 12 with the a-z, A-Z, 0-9 character set returns
    a 71-bit value. log_2((26+26+10)^12) =~ 71 bits

例子:

get_random_string()
u'ngccjtxvvmr9'

get_random_string(4, allowed_chars='bqDE56')
u'DDD6'

但如果你不想拥有Django这里是它的独立代码:

代码:

import random
import hashlib
import time

SECRET_KEY = 'PUT A RANDOM KEY WITH 50 CHARACTERS LENGTH HERE !!'

try:
    random = random.SystemRandom()
    using_sysrandom = True
except NotImplementedError:
    import warnings
    warnings.warn('A secure pseudo-random number generator is not available '
                  'on your system. Falling back to Mersenne Twister.')
    using_sysrandom = False


def get_random_string(length=12,
                      allowed_chars='abcdefghijklmnopqrstuvwxyz'
                                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
    """
    Returns a securely generated random string.

    The default length of 12 with the a-z, A-Z, 0-9 character set returns
    a 71-bit value. log_2((26+26+10)^12) =~ 71 bits
    """
    if not using_sysrandom:
        # This is ugly, and a hack, but it makes things better than
        # the alternative of predictability. This re-seeds the PRNG
        # using a value that is hard for an attacker to predict, every
        # time a random string is required. This may change the
        # properties of the chosen random sequence slightly, but this
        # is better than absolute predictability.
        random.seed(
            hashlib.sha256(
                ("%s%s%s" % (
                    random.getstate(),
                    time.time(),
                    SECRET_KEY)).encode('utf-8')
            ).digest())
    return ''.join(random.choice(allowed_chars) for i in range(length))
于 2015-02-13T00:32:01.690 回答
4

可以做一个生成器:

from string import ascii_uppercase
import random
from itertools import islice

def random_chars(size, chars=ascii_uppercase):
    selection = iter(lambda: random.choice(chars), object())
    while True:
        yield ''.join(islice(selection, size))

random_gen = random_chars(12)
print next(random_gen)
# LEQIITOSJZOQ
print next(random_gen)
# PXUYJTOTHWPJ

然后在需要时从生成器中拉出......在需要next(random_gen)时使用它们,或者使用random_200 = list(islice(random_gen, 200))例如......

于 2013-08-19T17:08:11.413 回答
0

对于加密的强伪随机字节,您可以使用围绕 OpenSSL的pyOpenSSL包装器。

它提供了bytes收集伪随机字节序列的功能。

from OpenSSL import rand

b = rand.bytes(7)

顺便说一句,12 个大写字母比 56 位熵多一点。您只需读取 7 个字节。

于 2013-08-19T17:36:04.367 回答
0
#!/bin/python3
import random
import string
def f(n: int) -> str:
        bytes(random.choices(string.ascii_uppercase.encode('ascii'),k=n)).decode('ascii')

对于非常大的 n 跑得更快。避免 str 连接。

于 2019-07-20T07:24:07.133 回答
0

此函数生成具有指定长度的随机大写字母字符串,

eg: length = 6,会生成如下随机序列模式

YLNYVQ

    import random as r

    def generate_random_string(length):
        random_string = ''
        random_str_seq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        for i in range(0,length):
            if i % length == 0 and i != 0:
                random_string += '-'
            random_string += str(random_str_seq[r.randint(0, len(random_str_seq) - 1)])
        return random_string
于 2018-02-24T17:53:30.060 回答