首页 > 其他 > 详细

random

时间:2019-05-05 19:45:17      阅读:136      评论:0      收藏:0      [点我收藏+]
>>> random.randrange(1,10) #返回1-10之间的一个随机数,不包括10 >>> random.randrange(1,10) 5 >>> random.randrange(1,10) 8 >>> random.randint(1,10) #返回1-10之间的一个随机数,包括10 >>> random.randint(1,10) 7 >>> random.randint(1,10) 10 >>> random.randrange(0, 100, 2) #随机选取0到100间的偶数 >>> random.randrange(1,10,2) 1 >>> random.randrange(1,10,2)#随机选取1到10间的基数 9 >>> random.randrange(0,10,2) 8 >>> random.randrange(0,10,2) 8 >>> random.randrange(0,10,2) 4 >>> random.random() #返回一个随机浮点数 >>> random.random() 0.019956638727846032 >>> random.random() 0.8488931922771353 >>> random.random() 0.3516909997856771 >>> random.choice(‘abce3#$@1‘) #返回一个给定数据集合中的随机字符 ‘#‘ >>> random.choice(‘abce3#$@1‘) ‘c‘ >>> random.choice(‘abce3#$@1‘) ‘@‘ >>> random.sample(‘abcdefghij‘,3) #从多个字符中选取特定数量的字符 [‘a‘, ‘d‘, ‘b‘] >>> random.sample(‘abce3#$@1‘,3) [‘c‘, ‘$‘, ‘b‘] >>> random.sample(‘abce3#$@1‘,3) [‘a‘, ‘$‘, ‘@‘] >>> "".join(random.sample(‘abce3#$@1‘,3)) ‘1ab‘ #生成随机字符串 >>> import string >>> ‘‘.join(random.sample(string.ascii_lowercase + string.digits, 6)) ‘4fvda1‘ #洗牌 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> random.shuffle(a) >>> a [3, 0, 7, 2, 1, 6, 5, 8, 9, 4]

random

原文:https://blog.51cto.com/10983441/2389428

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