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

random模块

时间:2018-04-23 18:36:11      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:color   输出   turn   多个   int   sam   验证   浮点   shuffle   

 1 import random
 2 
 3 print(random.random())           #随机输出0-1的浮点数
 4 print(random.randint(0,10))      #[0,10]随机输出一个整数,包括0和10
 5 print(random.randrange(0,10))    #[0,10)随机输出一个整数,包括0不包括10
 6 print(random.choice([1,2,3,5,4,6,9]))       #随机选取列表中顾的一个值
 7 print(random.sample([1,2,3,5,4,6,9],3))       #随机选取列表中的多个元素
 8 print(random.uniform(1,10))         #随机输出1,10的浮点数
 9 
10 l = [1,2,3,4,5,6,7,8,9]
11 random.shuffle(l)                 #打乱顺序
12 print(l)
13 
14 ##验证码
15 import random
16 def v_code():
17     ret = ‘‘
18     for i in range(4):
19         num = random.randint(0,9)
20         zimu = chr(random.randint(65,90))
21         choices = random.choice([num,zimu])
22         ret += str(choices)
23     return ret
24 
25 print(v_code())

 

random模块

标签:color   输出   turn   多个   int   sam   验证   浮点   shuffle   

原文地址:https://www.cnblogs.com/humanskin/p/8920462.html

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