首页 > 编程语言 > 详细

常用的python随机数

时间:2015-11-26 01:33:11      阅读:235      评论:0      收藏:0      [点我收藏+]

python中的随机数

模块:random

函数:

uniform :随机浮点数

>>> print random.uniform(0,100)
37.5204548309


randint :随机整数

>>> print random.randint(0,100)
4


randrange :随机偶数

>>> print random.randrange(0,101,2)
62


random :随机浮点数

>>> print random.random()
0.0472128549103


choice :随机字符

>>> print random.choice(‘qwertyuiop[];lkjhgfdsazxcvbnm,./‘)
t


sample :多个字符中选取特定数量的字符组成新的字符列表

>>> print random.sample(‘qwertyuiop[];lkjhgfdsazxcvbnm,./‘,3)
[‘]‘, ‘c‘, ‘z‘]


shuffle :洗牌

>>> item = [1,2,3,4,5,6]
>>> random.shuffle(item)
>>> item
[6, 5, 1, 3, 2, 4]


本文出自 “kysida” 博客,请务必保留此出处http://hongtengfei.blog.51cto.com/9881650/1716872

常用的python随机数

原文:http://hongtengfei.blog.51cto.com/9881650/1716872

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!