当前位置:首页>维修大全>综合>

怎么用python生成随机数(python生成100个随机数)

怎么用python生成随机数(python生成100个随机数)

更新时间:2024-06-24 18:06:41

怎么用python生成随机数

1本文需要加载的模块是:random

2给出0到10之间的随机整数:import randoma = random.randint(0,10)print(a)

3给出9到10之间的随机实数:import randoma = random.uniform(9,10)print(a)

4从9、19、29、39、……、99之间,随机选取一个实数:import randoma = random.randrange(9, 100, 10)print(a)

5从列表[5,6,7,8,9]里面,随机选取一个数:import randoma = random.choice([5,6,7,8,9])print(a)

6从一个字符串里面,随机选取一个字符:import randoma = random.choice("从一个字符串里面,随机选取一个字符!")print(a)

7随机打乱列表里面的字符顺序:import randoma = ["p","q","r","s","t","p","q","r","s","t","p","q","r","s","t",]random.shuffle(a)print(a)

8从列表里面随机选取9个数字:import randoma = range(3,100,2)b = random.sample(a, 9)print(b)

更多栏目