一、 random
所有关于随机相关的内容都在random模块中
二、相关功能
1. random()
- 产生(0, 1)之间的小数
import random print(random.random()) # 0.2622839912972649
2. uniform(a, b)
- 产生(a, b)之间的小数
import random print(random.uniform(3, 10)) # 3.1025846246337134
3. randint(a, b)
- 产生[a, b]之间的整数
import random print(random.randint(1, 10)) # 7
4. randrange(a, b, step)
- 产生[a:b:step]里的一个数
- step默认等于1
import random print(random.randrange(1, 10, 2)) # 3
5. choice(string/tuple/list)
- 产生对象里的其中一个元素
import random print(random.choice([11, 22, 33, 44, [55, 66, 77]])) # 44
6. sample(string/tuple/list/set)
- 从对象中任选两个元素,以列表形式返回
import random print(random.sample([11, 22, 33, 44], 2)) # [44, 22]
7. shuffle(list)
- 随机打乱顺序
- 返回None
import random lst = [11, 22, 33, 44, 55] random.shuffle(lst) print(lst) # [11, 55, 44, 33, 22]
继续阅读与本文标签相同的文章
下一篇 :
被机器人追杀好可怕?把门关上就好啦
-
PHP还有前途吗?
2026-05-19栏目: 教程
-
Python 制作微信全家福,你就是朋友圈最亮的仔!
2026-05-19栏目: 教程
-
3年java开发竟然还不知道Lambda的这个坑
2026-05-19栏目: 教程
-
小狗分类器,你家的狗子是个什么狗?
2026-05-19栏目: 教程
-
安卓开发开发规范手册V1.0
2026-05-19栏目: 教程
