 # Random Number List Divide Segments with Sum Threshold

Hey There,

Want to subdivide a list of random numbers so that within the subdivided partitions the sum of the numbers is as close to but not above an adjustable threshold. In practice the threshold would always be greater than the largest random number. For example:

Take a random list of numbers:

4
2
11
2
1
6
3
6
2
5
1
4
8
7

Subdivide to several lists with a maximum sum threshold.

Threshold = 12

4,2 (sum = 6)
11 (sum = 11)
2,1,6,3 (sum = 12)
6, 2 (sum= 8)
5, 1, 4 (sum = 10)
8 (sum = 8)
7 (sum = 7)

Is there a way to automate this process. Maybe a script?

Tried using Hoopsnake and Anemone but I’m stuck.

Would appreciate any help.

Thanks,

D

As I needed something simple, a little C#. Just hope there not errors !!

DataTree output = new DataTree();
int sum = 0;
int path = 0;
foreach(int val in x)
{
if ((sum + val) > limit)
{
sum = 0;
path += 1;
}
sum = sum + val;
}
A = output;

1 Like

Thank you so much! Works great. Knew this would be easy with a bit more coding knowledge!
-D

Is there a way to do the same thing but with decimals? I think integers would work fine for what I’m doing but just in case…

-D

Uhmm just change the int to double and it must work!!!