码迷,mamicode.com
首页 > 其他好文 > 详细

常用的Random函数

时间:2019-09-20 16:38:32      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:int   import   detail   ice   details   返回   结果   字符串   print   

一、random.random()
随机生成[0,1]的浮点数
import random
print("random.random:", random.random())


二、random.randint(min,max)
随机生成[min,max]范围内的整数
print("random.randint(1,10)", random.randint(1, 10))

 

三、random.choice()
从列表中随机选择一个元素
number_list = [1, 2, 3, 4, 5]
print("choice(list):", random.choice(number_list))


四、random.shuffle()
对列表元素进行随机排序, 无返回值
import random
number_list = [1, 2, 3, 4, 5]
random.shuffle(number_list)
print(number_list)

 

五、random.sample()
从(列表、元组、字符串)中随机获取指定数量的片段, 相当于执行了number次random.choice(), 返回的结果是列表类型
number_list = [1, 2, 3, 4, 5, 6]
print(random.sample(number_list, 3))

 

原文链接:https://blog.csdn.net/weixin_40576010/article/details/88379319

 



常用的Random函数

标签:int   import   detail   ice   details   返回   结果   字符串   print   

原文地址:https://www.cnblogs.com/wangyujian/p/11557843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!