Number interpolation and automatic reallocation.

tejan's icon

Hi all. I would like to have n amount of integers that all add up to a specific value and if any one of the n integers get changed, then that difference gets reflected, whether it's addition or subtraction in any one of the other integers. Preferably randomly. But I never want any number to dip below 1.

Example: I have 4 integer boxes and the specific value to be added up to is 16. The boxes are at 4 4 4 4. If I change the first one to 6, I'd like the change to be reflected and the boxes turn into 6 2 4 4. Let's say I change the fourth integer to 10. I'd like the change to reflected to something like 1 1 4 10. Thank you!

Source Audio's icon

if entry is only a single value to a single list member,

then you exclude that list member from sum

and change the rest to fit.

max list sum - this list member.

Question is how do you want to spread change among rest of the list.

you could use vexpr to keep distance of each list member from min/max values,

and when you need to add or subtract, simply send number of increases or decreases

to sorted indexes.

Or you use multislider set to floats and round scaled list output

correcting sum at the end to match set value.

Roman Thilenius's icon

as it seems he wants them to be randomized, and that would make things easy, as the whole taks is now only to create a set of random numbers which add up to a certain sum.

i would do the following:

(total - number changed by user) == sum
random of sum == A
random of (sum - A) == B
random of (sum - A - B) == C
scamble order of A B and C

Source Audio's icon

If list of remaining numbers to match the sum can be created randomly,

and completely free compared to previous list, including floor of 1

and max value one member can have, things would be easier.

But is that exactly what he wants ?

.....

.....

in any one means single item in the group.

if it can't make it alone , pick next one till sum matches.

At least that is what I understood.

In that few posted examples remaining list kept some members unchanged

in case picked one(s) had enough headroom in both directions

to make the difference.

"n amount of integers that all add up to a specific value... "

maybe he wants 2 random parameters to create the list,

list size and init value.

50 x 28 for example makes

sum od 1400, and value range 1 ~ 1351.

Pick random index and value within range to make a change,

then fix the sum using whatever rules.

either random, or pick sorted list to increase or decrease indexes etc.

something else ? who knows.

for example 50x28 :

if first modified value is 1000

that gives difference of +972 which needs to be

subtracted from 49 left list members.

or then created randomly from scratch.

having 400 points to share between them...

Roman Thilenius's icon

oh, i just notice my approach does not give you a uniform distribution, which would have been my intention.
however, if 2 are changed the same math could be used as when only one is changed. or i am not seeing something.

another question is whether negative numbers may appear or not. if yes, the maximum is range of signed long(64). which [rand] can not do.

Source Audio's icon

If I understand it correctly, floor is 1

so no negative numbers and input must be within range.

again , if I understand this right.

I would not bother with this till OP specifies what the rest of the list

needs to be made of.

Wil's icon

vexpr 'almost' works

except for that rounding error - sometimes

and weird triggering

floor 1 is ok

someone can do this math without floats?

Max Patch
Copy patch and select New From Clipboard in Max.


Roman Thilenius's icon

to correct myself, you´d get uniform distribution like so:

(total - number changed by user) == sum

random of (sum - 1) == a
random of (sum - 1) == b

sort list "a b"

a == A
b - a == B
sum - 1 - b == C

scamble order of A B and C