幽灵资源网 Design By www.bzswh.com

python 中random的常用方法总结

一、random常用模块

1.random.random() 随机生成一个小数

print(random.random())
 
# 输出
0.6060562117996784

2.random.randint(m,n) 随机生成一个m到n的整数(包括n)

print(random.randint(1, 5))
 
#输出
 
5

3. random.randrange(m,n) 随机生成m到n中的一个数,包括 m 但是不包括 n

print(random.randrange(1, 5))
 
# 输出
 
3

4. random.smaple(source,n) 在 source 中随机找出n个值,生成一个列表

print(random.sample(range(100), 5))
 
#输出
[27, 49, 21, 81, 45]

二、string 模块

 2.1 string.ascii_letters   # 所有的大小写英文字母

letters = string.ascii_letters
print(letters)
 
# 输出
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

2.2 string.ascii_lowercase # 所有的小写字母

2.3 string.ascii_uppercase # 所有的大写字母

2.4 string.digit # 1-9

2.5 string.punctuation  #特殊字符

sss = string.punctuation
print(sss)
 
# 输出
!"#$%&'()*+,-./:;<=>"htmlcode">
import random
import string
 
str_source = {
 1: string.ascii_lowercase,
 2: string.ascii_uppercase,
 3: string.digits,
 4: string.punctuation
}
 
check = []
 
for i in range(1, 5):
  y = random.sample(str_source[i], 1)
  check.append(y[0])
 
print("".join(check))
 
# 输出
bV5-

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

标签:
python,中random,python,random的使用方法

幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。