脚本专栏 发布日期:2025/10/26 浏览次数:1
Python3实现随机数
"htmlcode">
import random
#随机整数
import string
print(random.randint(0,99))
#随机选取0到100间的偶数
print(random.randrange(0, 101, 2))
#随机浮点数
print(random.random())
print(random.uniform(1, 10))
#随机字符
print(random.choice('abcdefg&#%^*f'))
#多个字符中选取特定数量的字符
print(random.sample('abcdefghij',3))
#多个字符中选取特定数量的字符组成新字符串
# print(string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).replace(" ",""))
#随机选取字符串
print(random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] ))
#洗牌
items = [1, 2, 3, 4, 5, 6]
random.shuffle(items)
print("洗牌:", items)
#从指定序列中随机获取k个元素作为一个片段返回,不会改变原有序列
list = []
list = random.sample(items,2)
print(list)
"//img.jbzj.com/file_images/article/201704/2017415142823318.png" alt="" />
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!