导语:
本文主要介绍了关于教你如何用Python生成随机数字和随机字符串的相关知识,包括男子为好玩将核酸结果P图为尿性,以及js生成随机字符串这些编程知识,希望对大家有参考作用。
直接看代码
随机整数:
>>> import random
>>> random.randint(0,99)
21
随机选取0到100间的偶数:
>>> import random
>>> random.randrange(0, 101, 2)
42
随机浮点数:
>>> import random
>>> random.random()
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
随机字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
>>> import random
random.sample('abcdefghij',3)
['a', 'd', 'b']
从多个字符中选择特定数量的字符组成一个新的字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python十六进制数代表什么09/17
- ♥ 十六进制在python中是什么意思09/03
- ♥ 如何按值的大小对 python3 字典进行排序?01/10
- ♥ python中的普通int和long int有什么区别09/21
- ♥ 如何用python3输出重复的数据?12/25
- ♥ 如何在python中检查变量的类型10/30
内容反馈