如何生成大小为N的字符串,由数字和大写英文字母组成,例如:

6个754z4英国U911K4型


当前回答

两种方法:

import random, math

def randStr_1(字符:str,长度:int)->字符串:chars*=math.ceil(长度/长度(chars))chars=字母[0:length]chars=列表(chars)随机.洗牌(字符)返回“”.join(字符)def randStr_2(字符:str,长度:int)->字符串:返回“”.join(随机.选择(字符)用于范围(字符)中的i)基准:从timeit导入timeitsetup=“”导入操作系统,子流程,时间,字符串,随机,数学def randStr_1(字母:str,长度:int)->str:letters*=math.ceil(长度/长度(字母))letters=字母[0:长度]letters=列表(字母)随机洗牌(字母)return“”.join(字母)def randStr_2(字母:str,长度:int)->str:return“”.join(范围(长度)中i的随机选择(字母))"""打印(“方法1 vs方法2”,“,每次运行10次。”)长度为[101001000100005000010005001000000]:打印(长度,'字符:')eff1=timeit(“randStr_1(string.asci_letters,{})”.format(长度),setup=设置,数字=10)eff2=timeit(“randStr_2(string.ascii_letters,{})”.format(长度),setup=setup,number=10)打印(“\t{}s:{}s”。格式(圆形(eff1,6),圆形(eff2,6)))print('\tradio={}:{}\n'.格式(eff1/eff1,round(eff2/eff1,2)))输出:方法1与方法2,每次运行10次。100个字符:0.00141秒:0.00179秒比率=1.0:1.271000个字符:0.013857秒:0.017603秒比率=1.0:1.2710000个字符:0.13426秒:0.151169秒比率=1.0:1.1350000个字符:0.709403秒:0.855136秒比率=1.0:1.21100000个字符:1.360735秒:1.674584秒比率=1.0:1.23500000个字符:6.754923秒:7.160508秒比率=1.0:1.061000000个字符:11.232965秒:14.223914秒比率=1.0:1.27第一种方法的性能更好。

其他回答

这个方法比Ignacio发布的random.choice()方法稍快,也稍令人讨厌。

它利用了伪随机算法的特性,按位和移位的存储体比为每个字符生成新的随机数更快。

# must be length 32 -- 5 bits -- the question didn't specify using the full set
# of uppercase letters ;)
_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'

def generate_with_randbits(size=32):
    def chop(x):
        while x:
            yield x & 31
            x = x >> 5
    return  ''.join(_ALPHABET[x] for x in chop(random.getrandbits(size * 5))).ljust(size, 'A')

…创建一个生成器,该生成器每次从0..31取出5位数字,直到没有剩余

…join()生成器对随机数的结果与正确的位

使用Timeit,对于32个字符串,计时为:

[('generate_with_random_choice', 28.92901611328125),
 ('generate_with_randbits', 20.0293550491333)]

…但对于64个字符串,randbits会丢失;)

除非我真的不喜欢我的同事,否则我可能永远不会在生产代码中使用这种方法。

edit:更新以适应问题(仅限大写和数字),并使用按位运算符&和>>而不是%和//

如果需要随机字符串而不是伪随机字符串,则应使用os.urandom作为源

from os import urandom
from itertools import islice, imap, repeat
import string

def rand_string(length=5):
    chars = set(string.ascii_uppercase + string.digits)
    char_gen = (c for c in imap(urandom, repeat(1)) if c in chars)
    return ''.join(islice(char_gen, None, length))
import string
from random import *
characters = string.ascii_letters + string.punctuation  + string.digits
password =  "".join(choice(characters) for x in range(randint(8, 16)))
print password

这是对Anurak Uniyal的回应的一种理解,也是我自己在研究的东西。

import random
import string

oneFile = open('‪Numbers.txt', 'w')
userInput = 0
key_count = 0
value_count = 0
chars = string.ascii_uppercase + string.digits + string.punctuation

for userInput in range(int(input('How many 12 digit keys do you want?'))):
    while key_count <= userInput:
        key_count += 1
        number = random.randint(1, 999)
        key = number

        text = str(key) + ": " + str(''.join(random.sample(chars*6, 12)))
        oneFile.write(text + "\n")
oneFile.close()

我使用此方法从a->z生成长度为n的随机字符串

输入随机数s=“”.join(随机选择([chr(i)for i in range(ord('a'),ord('z'))])for _ in range(10))