blankface
blankface

Reputation: 6347

Split a number into random number of parts where the sum of each part forms the number again

Let's say I have an integer 50. I'm trying to split this into a random number of parts so that the sum of each part form 50 again. E.g, if I split it in 4 parts: 20 + 10 + 15 + 5 = 50.

The number of parts (always less than the total amount) will have to be randomly generated and provided. How can I achieve this?

Upvotes: 2

Views: 3675

Answers (2)

Student
Student

Reputation: 719

from random import *


def break_num2(num, count, mode="int"):
    if mode == "float":
        ingreds = [num / count for _ in range(count)]
    elif mode == "int":
        ingreds = [num // count for _ in range(count)]
    while count:
        i1 = randrange(len(ingreds))
        i2 = (i1 + 1) % len(ingreds)
        if mode == "float":
            n = uniform(0, ingreds[i1])
        elif mode == "int":
            n = randint(0, ingreds[i1])
        ingreds[i1] -= n
        ingreds[i2] += n
        count -= 1
    if sum(ingreds) < num:
        ingreds.append(num - sum(ingreds))
    return ingreds

x = break_num2(50, 4)
y = break_num2(50, 4, "float")
print(x, sum(x), y, sum(y))

This prints for instance:

[12, 3, 4, 29, 2] 50 [0.8403666071897256, 36.636786293337956, 12.5, 0.02284709947231712] 50.0

Upvotes: 1

leaf_soba
leaf_soba

Reputation: 2518

python code:

def split_num():
    n = 50
    parts = random.randint(1,n)
    result = []
    for i in range(parts-1):
        x = random.randint(1,n-parts+i+1)
        n = n - x
        result.append(x)
    result.append(n)
    print(result)
    print(sum(result))

import random
split_num()

result:

[4, 33, 4, 1, 8]
50

Upvotes: 2

Related Questions