用random.randint。不会生成重复的随机数吗
来源:3-10 函数的其他三种使用技巧
慕仙234437
2020-04-24 22:55:40
random.randint,用for函数生成6个随机数字,
for i in range(0,6):
red = random.randint(1,33)
print(red,end=' ')
2回答
时间,
2020-04-25
同学,你好。在生成随机数时,是会出现随机数相同的情况的,机率是比较小的。同学可以参考下述代码去掉红球重复的情况
import random
for j in range(0, 6):
list1 = []
i = 0
while i < 6:
red = random.randint(1, 33)
# 判断生成的red在不在list1列表中
# 在则执行下一次循环,不在则添加到列表中
if red not in list1:
list1.append(red)
i += 1
else:
continue
blue = random.randint(1, 16)
print("红球:{}; 蓝球:{}".format(list1, blue))如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~
Vess_ji
2020-04-25
你需要引入 random这个包

相似问题