Python随机函数
来源:互联网 发布:搜狗网络推广登录 编辑:程序博客网 时间:2024/06/08 02:28
生成N个随机数:
list
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
slice
=
random.sample(
list
,
5
)
#从list中随机获取5个元素,作为一个片断返回
print
slice
print
list
#原有序列并没有改变。
随机整数:
>>> 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]
- Python 随机函数
- python sleep函数 随机
- Python 随机函数
- Python随机函数
- Python中的随机函数
- python生成随机函数
- python基础--随机函数
- Python随机函数
- python中的随机函数
- python中的随机函数random
- python常用随机函数及带权重随机实例
- python下的随机函数总结
- Python——随机函数(random)
- 随机函数
- 随机函数
- 随机函数
- 随机函数
- 随机函数
- Thrift的层次结构
- UVa 10925 - Krakovia
- Struts2中json的使用总结
- B3log Solo是什么
- 重新系统或VS2005,第一次编译wince6.0 报错问题
- Python随机函数
- 指针类型体验单步调试!
- jquery datepicker汉化
- 我们一起成长
- 分布式计算领域的哥德尔Eric Brewer
- greatest common divisor introduction to algorithm 3rd, example 31.2
- java下载文件的种方式
- ADB常用操作
- java日期类型转换总结date timestamp calendar string