Python生成1-150之间78个不重复随机数
以下是一种可能的方法:
- 定义一个空的列表 'num_list',用于存储生成的数。
- 使用 while 循环,重复以下步骤,直到 'num_list' 中包含 78 个不重复的数: a. 生成一个随机数 'num',范围为 1-150。 b. 如果 'num' 不在 'num_list' 中,将其添加到 'num_list' 中。
- 输出 'num_list'。
以下是 Python 代码实现:
import random
num_list = []
while len(num_list) < 78:
num = random.randint(1, 150)
if num not in num_list:
num_list.append(num)
print(num_list)
原文地址: https://www.cveoy.top/t/topic/lfbQ 著作权归作者所有。请勿转载和采集!