Python生成随机数random模块

import random

# 生成随机浮点数
float1 = random.uniform(10, 20)
float2 = random.uniform(1, 3)
# 生成随机整数[a,b]之间,闭区间
int1 = random.randint(10, 20)
int2 = random.randint(1, 3)
# 多个字符中选取特定数量的字符串
str1 = random.sample('abcdefghij', 3)# 得到list类型
# 随机字符
str3 = random.choice('abcdefg')
# 随机字符串
str4 = random.choice(['apple', 'pear', 'peach', 'orange', 'lemon'])
# 随机打乱#洗牌
items = [1, 2, 3, 4, 5, 6]
random.shuffle(items)

代码可直接复制运行。

猜你喜欢

转载自blog.csdn.net/u014571489/article/details/82927121