random模块中最常用的几个函数
转自:http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html
随机整数:
>>> import random
>>> random.randint(0,99)
21
随机选取0到100间的偶数:
>>> import random
>>> random.randrange(0, 101, 2)
42
随机浮点数:
>>> import random
>>> random.random()
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
随机字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
>>> import random
random.sample('abcdefghij',3)
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
最新文章
- 11、Linq的使用
- PRINCE2七大原则(1)
- 动态改变QSS
- 责任链模式(Chain of Responsibility Pattern)
- java-mina(nio 框架)
- 关于在MDK4.5以上版本不能使用JLINK V8的解决办法
- [LeetCode]题解(python):070-Climbing Stairs
- Apache+php+mysql+phpadmin搭建
- AFNetworking2.0和AFNetworking3.0 的HTTPS的配置
- 流程控制------if else分支语句
- [Spark内核] 第37课:Task执行内幕与结果处理解密
- 仓储repository概念
- video标签,在移动端获取第一帧作为展示
- linux系统下完全卸载Jenkins
- 批量找注入 python3+sqlmap结合
- 设计模式のChainOfResponsibilityPattern(责任链模式)----行为模式
- [No0000194]聊聊 Chrome DevTools 中你可能不知道的调试技巧
- 19 中山重现赛 1002 triangle
- 分享:android图片浏览器—类微信朋友圈相片浏览【android代码下载】
- hadoop源码学习(-)