测试结论:
random_str = lambda length: ''.join(random.choice(letters) for x in range(length))产生的随机字符串当然是会出现重复的。- secrets.randbits 产生随机的 xx位的整数,然后将整数映射到字符串中,产生随机字符串 也是会重复的
- 两个重复的概率差不多(手动小范围测试,一次产生 100 组,每组一万个随机字符串;大概会有 4 个重复的)
- secrets.randbits的方法要比 random_str 快很多,大概两、三倍 (分别产生长度为 8、10的随机字符串测试)
secrets.randbits 生成随机字符串:
def urandom_str(length=8):