Python3 数字
描述
choice() 方法返回一个列表,元组或字符串的随机项。
语法
以下是 choice() 方法的语法:
import random random.choice( seq )
注意:choice()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
参数
返回值
返回随机项。
实例
以下展示了使用choice() 方法的实例:
#!/usr/bin/python3 import random print ("从 range(100) 返回一个随机数 : ",random.choice(range(100))) print ("从列表中 [1, 2, 3, 5, 9]) 返回一个随机元素 : ", random.choice([1, 2, 3, 5, 9])) print ("从字符串中 'Runoob' 返回一个随机字符 : ", random.choice('Runoob'))
以上实例运行后输出结果为:
从 range(100) 返回一个随机数 : 68 从列表中 [1, 2, 3, 5, 9]) 返回一个随机元素 : 2 从字符串中 'Runoob' 返回一个随机字符 : u
以下是一个随机密码生成器的简单实现:
import random import string def generate_password(length): # 定义密码可用字符集合 chars = string.ascii_letters + string.digits + string.punctuation # 随机选择字符生成密码 password = ''.join(random.choice(chars) for _ in range(length)) return password random_pwd = generate_password(6) # 输出长度为 6 print(random_pwd)
以上实例中 generate_password() 函数接受一个整数参数 length,表示要生成的密码长度,然后利用 Python 标准库中的 random 和 string 模块生成随机密码。string.ascii_letters 包含所有字母(大写和小写),string.digits 包含所 有数字,string.punctuation 包含所有标点符号。
random.choice(chars) 会从字符集chars中随机选择一个字符,然后 join() 方法会将生成的字符拼接在一起形成密码。
R?u|<K
笔记
创建随机密码组合:
import string #string module里包含了阿拉伯数字,ascii码,特殊符号 import random #需要利用到choice a = int(input('请输入要求的密码长度')) b = string.digits + string.ascii_letters + string.punctuation #构建密码池 password = "" #命名一个字符串 for i in range(0,a): #for loop 指定重复次数 password = password + random.choice(b) #从密码池中随机挑选内容构建密码 print(password) #输出密码