Python中random模块的用法案例
2024-10-10 20:10:32
1 import random # 调用random模块
2
3 a = random.random() # 随机从0-1之间抽取一个小数
4 print(a)
5
6 a = random.randint(0,100) # 随机从0-100之间抽取一个数字
7 print(a)
8
9 a = random.choice('abcdefg') # 随机从字符串/列表/字典等对象中抽取一个元素(可能会重复)
10 print(a)
11
12 a = random.sample('abcdefg', 3) # 随机从字符串/列表/字典等对象中抽取多个不重复的元素
13 print(a)
14
15 items = [1, 2, 3, 4, 5, 6] # “随机洗牌”,比如打乱列表
16 random.shuffle(items)
17 print(items)
最新文章
- WCF学习之旅—TcpTrace工具(二十六)
- Tween公式 以及四个参数
- C#基础---Attribute(标签) 和 reflect(反射) 应用
- 浅入浅出“服务器推送”之一:Comet简介
- dedecms调用标签总结(一)
- pycharm安装
- 24点C++程序实现 编程之美1.16
- Start of Something New
- 导入 from pdfminer.pdfinterp import process_pdf 错误
- 记一次令人发狂的 bug Eclipse 开不开 tomcat 7.0
- python下module、package导入
- Android定位功能
- 异常:必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]
- How to setup Eclipse with WinAVR and the Eclipse plugin AVR-eclipse
- 【swift-总结】函数
- Django__WSGI
- 越来越火的";中台";是什么
- k-近邻(KNN)算法改进约会网站的配对效果[Python]
- s21day08 python笔记
- swiper 支持性