random_test.py 520 B

123456789101112131415161718192021222324252627
  1. import random
  2. def random_amount(count, total, min_unit=1):
  3. amount = total / min_unit
  4. arr = [0] * count
  5. v = i = 0
  6. while i < count:
  7. r = random.randint(11, 99) / 100
  8. arr[i] = r
  9. v += arr[i]
  10. i += 1
  11. ratio = 1.0 / v
  12. result = [0] * count
  13. b = total
  14. for j in range(0, count - 1):
  15. result[j] = round(round(arr[j] * ratio * amount) * min_unit)
  16. b -= result[j]
  17. result[count - 1] = b
  18. return result
  19. def test():
  20. print(random_amount(3, 6))