## separate an integer….

Mar 11, 2009 at 12:05am

# separate an integer….

Hi list,

i am looking for an object for separate an integer in several integer…

I explain :

for exemple I would separate 256 in 7 parts not equal. I would the objects take me a list with 7 members and the total of this number be equal to 256.

somebody could help me or say me where I could find that??

thanks a lot

M

#42780
Mar 11, 2009 at 1:10am

maxd wrote on Tue, 10 March 2009 17:05Hi list,

i am looking for an object for separate an integer in several integer…

somebody could help me or say me where I could find that??

Programming, dear Watson. Paste this into your max 5 patch.

– Pasted Max Patch, click to expand. –
#153158
Mar 11, 2009 at 1:19am

WOW ! great !!

thanks a lot

m

#153159
Mar 11, 2009 at 2:33am

That’s very cool mzed! So simple when it all works, yet these are the little things that confuse me sometimes when patching. Couldn’t help messing around with it a bit, so here’s a different take, using recursion instead. In this case, though, the total number of integers at the end is determined by what random numbers are picked along the way, so you can’t control that (though you could check list lengths with zl len and choose whether to use certain ones as they happen).

I like seeing the many different ways certain numbers can be summed to, it’s interesting and you can get a feel for the various combinations. plus it gives your addition skills a little workout…

– Pasted Max Patch, click to expand. –
#153160
Mar 11, 2009 at 2:17pm

Hi list,

thanks you this patch helps me a lot….

but if I would make some separations at least 20….

like : 20+56+32+46+43++23+36 = 256

how could I do that???

it’s a little more complicated….and i don’t find the solution.

Thanks

#153161
Mar 12, 2009 at 12:36pm

nobody?

thx

#153162
Mar 12, 2009 at 2:12pm

Here’s a messy bit of javascript that will do what you want. I’ll have a look at tidying it up later but you should manage ok for now. The first inlet is the number you want to slice up. The second is the number of items you want in the output list and the third is a range value, for example setting it to 4 will limit the numbers to being +/-2 about the average. If you set this number too large you will end up getting zeroes and negative numbers which you probably don’t want. I hope it helps.

lh

// lh.splitsum
inlets = 3;
var num = jsarguments[1];
var range = jsarguments[2];

function msg_int (x)
{
if (inlet == 0)
{
total = x;
listemup = [];
count = num;
previn = 0;
for (i=0; i
{
ave = x/count–;
rand = Math.round((Math.random()*range)+ave-(range*0.5));
x = x-rand;
listemup.push (rand);
}
listemup.forEach(lastone);
listemup.push(total-previn);
outlet (0, listemup);
}
else if (inlet == 1)
num = x;
else if (inlet == 2)
range = x;

function lastone (count)
{
previn += count;
}
}
//EOF

———-begin_max5_patcher———-
440.3ocyUs0aCBBE9Y8WAgmcMBTs5da+NVZV7BqiFEMBl00l9ee.pocc1nyt
d4EHb.Nmuy241NaKXbwFp.BdF7JvxZmskkQjVfU6YKXdzljrHg4YvbpPDshB
cZtSR2HMxwd9cxJijIev3qdqhlHazMwclqCX9B8JAOyyAfBl4BV19i2K3RdT
N0noWpXQYc5hWmy3YTow33CBKpkcRQGoDAaqQIHrxRMhYoFsVDu9ILtSsMeW
9UIsAePHXo9l8115EmQRFJfDSqFzuQHO8lm4.18u63ndcb7ncbj+YcbFWBc.