Python 随机函数
来源:互联网 发布:在淘宝网买东西可靠吗 编辑:程序博客网 时间:2024/06/05 17:11
随机整数:
>>> 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]
random的函数还有很多,此处不一一列举。
>>> 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]
random的函数还有很多,此处不一一列举。
- Python 随机函数
- python sleep函数 随机
- Python 随机函数
- Python随机函数
- Python中的随机函数
- python生成随机函数
- python基础--随机函数
- Python随机函数
- python中的随机函数
- python中的随机函数random
- python常用随机函数及带权重随机实例
- python下的随机函数总结
- Python——随机函数(random)
- 随机函数
- 随机函数
- 随机函数
- 随机函数
- 随机函数
- Mongodb c Driver 使用实例
- Eclipse 在开发中使用到的快捷键很实用噢(转载)
- vb.net c# 转换
- 判断奇数并打印三角形
- 如何删除 SQL Server 表中的重复行(1)
- Python 随机函数
- UITableView多选删除,类似mail中的多选删除效果
- 主流Java Web服务器(应用服务器)
- Pydev的安装和设置
- 面试体会
- 进程和线程的联系和区别
- Shell Parsing Process
- Python截取路径中的文件名
- 实用控件:将任意文件打包进dfm(zlib压缩)的控件