根据给定的总数,按照用户需求随机生成各列的数值数据
import random
"""
根据给定的总数,随机生成符合各列范围限制的数。
"""
def split_sum(total):
"""
根据总分 total 随机生成一组符合各列范围限制的分数(共13列),且总和等于 total。
每列的取值范围如下:
[
(1,5), (1,5), (1,5), (1,5),
(1,10), (1,5), (1,5), (1,10),
(1,10), (1,10), (1,10), (1,10), (1,10)
]
"""
# 每列的最小最大值范围(共13列)
ranges = [
(3, 5), (3, 5), (2, 5), (2, 5), (5, 10), (3, 5), (2, 5), (7, 10), (7, 10), (8, 10), (6, 10), (10, 10), (7, 10)
]
# 初始化为每列最小值
values = [r[0] for r in ranges]
min_total = sum(values)
if total < min_total:
raise ValueError(f"总数 {total} 小于最小可能的总和 {min_total}.")
max_total = sum(r[1] for r in ranges)
if total > max_total:
raise ValueError(f"总数 {total} 超过最大可能的总和 {max_total}.")
remaining = total - min_total
# 随机选择列进行分配,直到用完所有剩余分数
while remaining > 0:
i = random.randint(0, len(values) - 1) # 随机选择一列
add = min(ranges[i][1] - values[i], remaining)
if add > 0:
values[i] += add
remaining -= add
return values
def process_totals(totals):
results = []
for total in totals:
try:
result = split_sum(total)
results.append(result)
except ValueError as e:
print(f"跳过总数 {total}: {e}")
results.append([0] * 13) # 返回 13 个 0 表示错误情况
return results
# 总分数据
totals = [
72, 70, 79, 76, 83, 78, 82, 80, 81, 88,
76, 75, 74, 73, 74, 74, 76, 75, 80, 78,
76, 74, 88, 86, 76, 76, 88, 72, 73, 60,
76, 77, 74, 72, 76, 78, 74, 75, 73
]
results = process_totals(totals)
# 打印结果
for i, result in enumerate(results):
print(f"总数为: {totals[i]} 时的最终值为: {result}")