随机数Random模块

随机数random模块

import random

#打印10~100任一随机整数,不包括100
print(random.randint(10,100))

# 打印1~10任一随机整数,包括10;
print(random.randrange(1,10,2))  # 步长

#输出0~1的随机浮点数
print(random.random())

print(random.choice('abc!@#$%')) # 返回一个给定数据集中的随机字符
--------------------------------------------------------------------
70
5
0.7106090220574072
a

print(random.sample('abcdfghi',3))  # 随机输出3个字符串
----------------------------------------------------------------------
['h', 'g', 'b']



import string
print(string.ascii_lowercase)
print(string.digits+string.ascii_lowercase)
print("".join(random.sample(string.digits+string.ascii_lowercase,5)))   #输出随机验证码
----------------------------------------------------------------------
abcdefghijklmnopqrstuvwxyz
0123456789abcdefghijklmnopqrstuvwxyz
y4f2c


# 洗牌

a =list(range(100))
random.shuffle(a)
print(a)

--------------------------------------------------------------------
[2, 30, 62, 67, 8, 77, 63, 59, 16, 20, 90, 47, 23, 1, 7, 41, 65, 55, 80, 75, 27, 35, 83, 53, 34, 15, 57, 25, 49, 19, 0, 52, 92, 94, 37, 21, 84, 97, 31, 71, 42, 17, 29, 93, 44, 28, 43, 81, 88, 6, 98, 9, 39, 69, 89, 45, 72, 24, 87, 70, 86, 60, 14, 26, 48, 38, 85, 12, 4, 91, 73, 58, 11, 50, 64, 56, 5, 10, 99, 18, 96, 36, 32, 54, 74, 40, 68, 13, 82, 22, 61, 78, 66, 79, 33, 51, 76, 46, 3, 95]



原文地址:https://www.cnblogs.com/chenfei2928/p/12777231.html