fork download
  1. # ====================== 配置区 ======================
  2. total = 113.25 # 数量总和
  3. price = 62 # 单价
  4. max_diff = 1.5 # 数量最大相差 ≤1.5
  5. allow_over = 3 # 仅允许 3 个金额超 1000 元
  6. # ====================================================
  7.  
  8. import random
  9. random.seed()
  10.  
  11. # 自动计算
  12. max_normal_qty = 1000 / price # 不超1000元的最大数量
  13. count = 9 # 固定9个数字
  14. base = total / count
  15. nums = []
  16. used_values = set()
  17. over_count = 0
  18.  
  19. # 生成前 8 个数字
  20. for _ in range(count - 1):
  21. # 随机生成,范围紧凑保证差值合规
  22. if over_count < allow_over:
  23. # 生成超1000元的数量
  24. val = round(random.uniform(max_normal_qty + 0.1, max_normal_qty + 0.8), 2)
  25. over_count += 1
  26. else:
  27. # 生成不超1000元的数量
  28. val = round(random.uniform(base - 0.7, base + 0.7), 2)
  29.  
  30. # 确保不重复
  31. while val in used_values:
  32. val = round(val + 0.01, 2)
  33. used_values.add(val)
  34. nums.append(val)
  35.  
  36. # 第9个数字自动补齐总和
  37. last_num = round(total - sum(nums), 2)
  38. while last_num in used_values:
  39. last_num += 0.01
  40. last_num = round(last_num, 2)
  41. nums.append(last_num)
  42.  
  43. # 最终校准总和
  44. final_sum = sum(nums)
  45. if abs(final_sum - total) > 0.001:
  46. nums[-1] = round(nums[-1] + total - final_sum, 2)
  47.  
  48. # 打乱顺序,彻底无规律
  49. random.shuffle(nums)
  50.  
  51. # ===================== 输出 =====================
  52. for num in nums:
  53. print(f"{num:.2f}")
  54.  
  55. # 校验输出
  56. amounts = [round(x * price, 2) for x in nums]
  57. print("\n===== 校验结果 =====")
  58. print(f"✅ 数量总和:{sum(nums):.2f}")
  59. print(f"✅ 超1000元个数:{len([a for a in amounts if a > 1000])}")
  60. print(f"✅ 最大相差:{max(nums)-min(nums):.2f} ≤ 1.5")
  61. print(f"✅ 无重复:{len(set(nums)) == len(nums)}")
Success #stdin #stdout 0.03s 11640KB
stdin
Standard input is empty
stdout
12.42
13.04
1.55
11.97
16.24
12.40
16.45
12.87
16.31

===== 校验结果 =====
✅ 数量总和:113.25
✅ 超1000元个数:3
✅ 最大相差:14.90 ≤ 1.5
✅ 无重复:True