Python随机数、随机字符的生成


import random
"""python随机数的生成"""

# 随机产生0-1的小数
print(random.random())

# 随机产生1-5的整数
print(random.randint(1, 5))

# 随机产生0-5的浮点数
print(random.uniform(0, 5))

# 以指定步长产生随机整数,例如以2为步长,相当于产生一个偶数
print(random.randrange(0, 10, 2))

# 从特定序列中随机选取一个字符,该序列可以为列表、字符串、元组等
string = "abcdfghijklmnopqrstuvwxyz"
print(random.choice(string))

# 从特定序列中随机选取指定长度的序列
lines = ['this', 'is', 'my', 'love']
print(random.sample(lines, 2))

# 将顺序打乱,洗牌
lines = [1, 2, 3, 4, 5]
random.shuffle(lines)
print(lines)
发布了22 篇原创文章 · 获赞 17 · 访问量 5854

猜你喜欢

转载自blog.csdn.net/weixin_42193813/article/details/104132740