当前位置: 移动技术网 > IT编程>脚本编程>Python > Python random 模块

Python random 模块

2018年04月19日  | 移动技术网IT编程  | 我要评论

pt950铂金戒指价格,st45.8,琳达汽车资讯

import random

# 方法返回随机生成的一个实数,它在[0,1)范围内
print(random.random())
运行结果:
0.06435148447021877


# 方法返回随机生成的一个整数,这里包括 8
print(random.randint(1, 8))
运行结果:
1


# 返回一个列表,元组或字符串的随机项
print(random.choice('hello'))
运行结果:
l

print(random.choice(['hello', 11, [22]]))
运行结果:
[22]


# 从 list 中随机获取 2 个元素,作为一个列表返回  
print(random.sample(['123', 4, [1, 2]], 2))
运行结果:
['123', [1, 2]]


# 随机获取5个验证码
def v_code():
    code = ''
    for i in range(5):
       code += str(random.choice([random.randint(0, 9), chr(random.randint(65, 91))]))

    print(code)

v_code()
运行结果:
33NSI

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网