Python笔记之random模块!
程序员文章站
2024-01-03 12:09:22
#random模块import randomimport stringprint(random.randint(10,100)) print(random.randrange(10,100,5)) #数字5是步长print(random.random()) #返回随机浮点数print(random.choice('asdasdafafaf')) #返回字符串的随机一个print(random.sample(range(1,31),5)) #从多个对象中随机返回指定个对象p...
#random模块
import random
import string
print(random.randint(10,100))
print(random.randrange(10,100,5)) #数字5是步长
print(random.random()) #返回随机浮点数
print(random.choice('asdasdafafaf')) #返回字符串的随机一个
print(random.sample(range(1,31),5)) #从多个对象中随机返回指定个对象
print(string.digits + string.ascii_lowercase)
print(''.join(random.sample((string.digits + string.ascii_lowercase),5))) #随机生成网站验证码
a = list(range(10))
random.shuffle(a) #打乱原有列表的顺序
print(a)
输出结果:
49
35
0.5636116539682788
a
[3, 15, 28, 16, 12]
0123456789abcdefghijklmnopqrstuvwxyz
ajxen
[3, 0, 9, 1, 5, 2, 7, 4, 8, 6]
Process finished with exit code 0
本文地址:https://blog.csdn.net/Kinght_123/article/details/110854970