Python学习记录W05-06:内置模块详解之Random模块

import random

#随机整数:
print(random.randint(0,99))

#随机选取0到100间的偶数:
print(random.randrange(0,101,2))

#随机浮点数:
print(random.random())
print(random.uniform(1,10))

#随机字符:
print(random.choice('abcdefg&#%^*f'))

#多个字符中选取特定数量的字符:
print(random.sample('abcdefghij',3))  #['f','h','d']

#随机选取字符串:
print(random.choice(['apple','pear','peach','orange','lemon'])) #apple
#洗牌#
items = [1,2,3,4,5,6,7]
print(items)
random.shuffle(items)
print(items)

猜你喜欢

转载自blog.csdn.net/up1292/article/details/82500598