generate_sum_of_numbers.py 440 B

1234567891011121314151617181920212223
  1. import random
  2. def generate_random_integers(_sum, n):
  3. mean = _sum / n
  4. variance = int(0.25 * mean)
  5. min_v = mean - variance
  6. max_v = mean + variance
  7. array = [min_v] * n
  8. diff = _sum - min_v * n
  9. while diff > 0:
  10. a = random.randint(0, n - 1)
  11. if array[a] >= max_v:
  12. continue
  13. array[a] += 1
  14. diff -= 1
  15. print(array)
  16. # sum of 30
  17. # 5 of them
  18. generate_random_integers(30, 5)