python之random模块

from random import *

random1 = random()
print(type(random1), random1)  # [0, 1)
randint1 = randint(2, 100)  # [2,100]
print(type(randint1), randint1)
result = [3, 45, 2, 1, -8]
sample1 = sample(result, 2)
print(type(sample1), sample1)
rr = randrange(2, 10, 2)  # 结果为2 4 6 8 中的其中一个
print(type(rr), rr)


def v_code(char_count):
    chs = list(range(48, 58)) + list(range(65, 91)) + list(range(97, 123))
    result = ''
    for i in range(char_count):
        result += chr(choice(chs))
    return result


print(v_code(5))

输出:(结果是随机的,下面只是一次运行的结果,仅做参考)

<class 'float'> 0.6281382442051219 
<class 'int'> 64
<class 'list'> [45, 2]
<class 'int'> 2
AZwq9

原文地址:https://www.cnblogs.com/zhaoxianglong1987/p/7569308.html