import random def random_amount(count, total, min_unit=1): amount = total / min_unit arr = [0] * count v = i = 0 while i < count: r = random.randint(11, 99) / 100 arr[i] = r v += arr[i] i += 1 ratio = 1.0 / v result = [0] * count b = total for j in range(0, count - 1): result[j] = round(round(arr[j] * ratio * amount) * min_unit) b -= result[j] result[count - 1] = b return result def test(): print(random_amount(3, 6))