取随机数python
取一个随机浮点数。dom
import random random.random()
0.9896051007555908
a=random.randint(1,3) b=random.randint(1,3) c=random.randint(1,3) print(a,b,c)
3 1 2
e=random.choice('hello') f=random.choice('hello') g=random.choice('hello') print(e,f,g)
e l l
seq1=random.sample('hello',2) seq2=random.sample('hello',2) seq3=random.sample('hello',2) print(seq1,seq2,seq3)
['l', 'h'] ['l', 'l'] ['e', 'h']
ls1=[1,2,3,4,5,6] random.shuffle(ls1) print(ls1)
[5, 2, 6, 3, 4, 1]
checkcode='' for i in range(4): current=random.randint(0,9) checkcode+=str(current) print(checkcode)
0346
import random checkcode1='' # 验证码寄存 for i in range(0,3): #三次 产生三组验证码,检验是否能生成 for i in range (4):#执行次,产生4个随机字符 current=random.randrange(0,4)#随机深沉一个数字和 if current == i: # 用随机数和循环次数猜 tmp=chr(random.randint(65,90))# 猜中了几生成一个字符 else: tmp=random.randint(0,9)# 没有猜中就生成一个数字 checkcode1+=str(tmp) #将生成的字母或者数字添加到变量里面 print('验证码:',checkcode1)# 将生成的验证码打印出来 checkcode1='' # 初始化验证码寄存器
验证码: 0497 验证码: 5C12 验证码: 3O3R
```code