import random ret = random.random() # 随机浮点数 random.random() ret = random.randint(1,21) # 随机范围整数 randint() ret = random.choice([11,22,33]) # 随机取一个 choice() ret = random.sample([11,22,33],2) # 随机取两个数 sample() ret = random.uniform(1,2) # 随机指定范围浮点型 uniform() item = [1,2,3,4] random.shuffle(item) # 打乱列表顺序 shuffle() print(item)
# *********** 随机验证码 import random def v_code(): ret="" for i in range(4): num = random.randint(0,9) # 随机0-9数字 a = chr(random.randint(65,122)) # chr 将数字转为ASCII码 65-122为大小写字母 s = str(random.choice([num, a])) # 随机一个取数字或字母 ret += s # 拼接字符串 return ret print(v_code())