python随机数用法 转载自https://www.cnblogs.com/skyEva/p/6097157.html
来源:互联网 发布:软件测试培训 编辑:程序博客网 时间:2024/04/29 23:28
Python 随机数用法
1. random.seed(int)
- 给随机数对象一个种子值,用于产生随机序列。
- 对于同一个种子值的输入,之后产生的随机数序列也一样。
- 通常是把时间秒数等变化值作为种子值,达到每次运行产生的随机系列都不一样
- seed() 省略参数,意味着使用当前系统时间生成随机数
1
2
3
4
5
6
7
8
9
10
random.seed(
10
)
print
random.random()
#0.57140259469
random.seed(
10
)
print
random.random()
#0.57140259469 同一个种子值,产生的随机数相同
print
random.random()
#0.428889054675
random.seed()
#省略参数,意味着取当前系统时间
print
random.random()
random.seed()
print
random.random()
2. random.randint(a,b)
- 返回指定范围的一个随机整数,包含上下限
1
print
random.randint(
1
,
10
)
3. random.uniform(u,sigma)
- 随机正态浮点数
1
print
random.uniform(
1
,
5
)
4. random.randrange(start,stop,step)
- 按步长随机在上下限范围内取一个随机数
1
print
random.randrange(
20
,
100
,
5
)
5. random.random()
- 随机浮点数
1
print
random.random()
6. 随机选择字符
- 随机的选取n个字符
1
print
random.sample(
'abcdefghijk'
,
3
)
- 随机的选取一个字符
1
print
random.choice(
'abcde./;[fgja13ds2d'
)
- 随机选取几个字符,再拼接成新的字符串
1
print
string.join(random.sample(
'abcdefhjk'
,
4
)).replace(
" "
,"")
7.random.shuffle
- 对list列表随机打乱顺序,也就是洗牌
- shuffle只作用于list,对Str会报错比如‘abcdfed’,而['1','2','3','5','6','7']可以
1
2
3
4
5
6
7
8
9
item
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
]
print
item
random.shuffle(item)
print
item
item2
=
[
'1'
,
'2'
,
'3'
,
'5'
,
'6'
,
'7'
]
print
item2
random.shuffle(item2)
print
item2
阅读全文
0 0
- python随机数用法 转载自https://www.cnblogs.com/skyEva/p/6097157.html
- 转载logback简要用法 原文地址https://www.cnblogs.com/warking/p/5710303.html
- [Python] heapq简介(转载自:http://www.cnblogs.com/lexus/p/3324991.html)
- redis学习命令大全(转载https://www.cnblogs.com/woshimrf/p/5198361.html)
- https://www.cnblogs.com/sea-star/p/rtcp.html
- https://www.cnblogs.com/by-dream/p/5611555.html
- https://www.cnblogs.com/Y1Focus/p/6707121.html
- Android Studio导入Fresco--转载自http://www.cnblogs.com/stay/p/4398432.html
- 转载自 http://www.cnblogs.com/ITtangtang/p/3926665.html,Log4J
- slf4j的个人学习--转载自http://www.cnblogs.com/xing901022/p/4149524.html
- jvm调优转载自http://www.cnblogs.com/xingzc/p/5756119.html
- redis的使用(转载自:http://www.cnblogs.com/edisonfeng/p/3571870.html)
- 转载自http://www.cnblogs.com/pinard/p/5970503.html---梯度下降讲解
- 转载自http://www.cnblogs.com/whgk/p/6074930.html 本文hibernate教程
- 转载自http://www.cnblogs.com/whgk/p/6616593.html 本文Spring
- python数据类型详解(转自:http://www.cnblogs.com/linjiqin/p/3608541.html)
- HTTPS://WWW.CNBLOGS.COM/ANDROID-JOKER/P/4414891.HTML https://www.cnblogs.com/android-joker/p/4414891
- 转载http://www.cnblogs.com/kenshincui/p/3931948.html
- hdoj1041
- unity引擎基础
- phpstrom远程连接linux系统上的项目
- 001
- CDC textout函数关于CString对象的输出问题
- python随机数用法 转载自https://www.cnblogs.com/skyEva/p/6097157.html
- 结合OPENCV SIFT代码浅谈Lowe的一些SIFT算法细节
- Git使用中的两三事
- 分治算法——假硬币寻找,硬币找零,众数问题
- 深入理解Java面向对象三大特性 封装 继承 多态
- Python-可变对象和不可变对象
- 存储器层次结构4
- CodeForces 876B Divisiblity of Differences(思维 + 数学)
- linux安装meven