打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python随机数与随机字符串详解
随机整数:
1
2
3
>>> import random
>>> random.randint(0,99)
21
随机选取0到100间的偶数:
1
2
3
>>> import random
>>> random.randrange(0, 101, 2)
42
随机浮点数:
1
2
3
4
5
>>> import random
>>> random.random()
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
随机字符:
1
2
3
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
1
2
3
>>> import random
random.sample('abcdefghij',3)
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
1
2
3
4
5
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
1
2
3
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
1
2
3
4
5
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
random的函数还有很多,此处不一一列举,
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python生成8位随机字符串的一些方法
string-random 生成随机字符串 NPM 包
DELPHI生成随机字符串
Struts2实现的6位数字的验证码程序
编程新手:看懂很多示例,却依然写不好一个程序
网站上的验证码是怎么产生的?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服