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

4-6 随机数

时间:2018-11-29 01:35:01      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:随机   color   style   port   sample   span   random   div   rand   

1、随机整数

1 import random
2 print(random.randint(1,23))#随机整数
3 
4 l=[1,2,3,4]
5 s=abcdefeg
6 print(random.choice(s))  # 随机选择一个元素
7 print(random.sample(s,3))  # 随机选择几个元素
8 random.shuffle(l)  # 只能传list,打乱顺序
9 print(l)

 

2、随机小数

1 import random
2 
3 f = random.uniform(1,19) #取随机的小数
4 print(f)  # 9.37672854445731
5 newf = round(f,3) # 取几位小数
6 print(newf)  # 9.377

 

4-6 随机数

标签:随机   color   style   port   sample   span   random   div   rand   

原文地址:https://www.cnblogs.com/hushaoyan/p/10035666.html

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