python_random模块
random模块主要用于产生随机数
1、random
生成随机数几种方法,如下:
import string import random passwd=random.randint(1,23) #随机产生1-23之间的一个随机数,包含1和23 print(passwd) passwd2=random.sample(string.ascii_letters,5) #随机产生5个字母(大写或小写) print(passwd2) passwd3=random.choice(string.ascii_letters) #随机产生1个字母(大写或小写) print(passwd3) passwd4=random.choices([1,2,3],weights=[10,20,30]) #按权重随机产生1个数字 print(passwd4) D:\study\python\test\venv\Scripts\python.exe D:/study/python/test/dd.py 15 ['V', 'a', 'n', 'P', 'd'] O [2]
2、产生小数及list洗牌
import random print(random.uniform(1,999)) #产生1-999之间的小数 l=[1,2,3,4] random.shuffle(l) #洗牌 只能传list,且无返回值 print(l) D:\study\python\test\venv\Scripts\python.exe D:/study/python/test/dd.py 876.0676444884089 [2, 3, 1, 4]