Random division of a number

Feb 3, 2010 at 9:39pm

Random division of a number

I am trying to find a more or less elegant way to distribute a random number of random variables that all add up to a fixed number. Maybe there is a fancy sigma formula out there, or maybe there is a dead easy way that I have overlooked.

For example: I would like to divide the number 20 into whatever: (4, 5, 9, 2), (10, 10), (9, 6, 5) and so on.

Any ideas?

Thanks a bunch

Eskil

#48203
Feb 3, 2010 at 10:26pm

Depends on what you want the spread to look like. You could start with your sum number, take a [random] number in the range, subtract from the sum, do again and again until you reach it. Though I think the distribution would have more small numbers this way.

The other way might be to determine the spread you want for the actual numbers, then just take random values in that spread until you meet or exceed your sum, and adjust the last chosen value to ensure the correct sum.

[accum] if you’re generating them one by one, [zl sum] if you make lists and then sum them.

A recent thread was somewhat similar:

http://cycling74.com/forums/topic.php?id=24386

The example patches at the bottom may help sort out some techniques. You do need to be careful anytime you have a recursive process, as you will with this one: “If the sum isn’t my maximum yet, do the loop again”. Save often, and save before hooking up that recursive patchcord…

#173337
Feb 4, 2010 at 8:57am

Hello maxers,

here a way ; must be improved.

– Pasted Max Patch, click to expand. –
#173338
Feb 4, 2010 at 2:29pm

I love these kind of threads, lots of Max beauty to be found. Good patch, Vanille, quite ingenious. Possible remarks would be that the correction of the float rounding is problematic. When the sum of the rounded integer list is more than 1 more than the total of 20, it’s possible to get a wrong output (sel 0 doesn’t suffice in that case).

I tried sending “0.66 0.66 0.66 0.66 8.68 8.68″ into [p INTEGER_SUM_20]. You get frequent outputs of -1 in the result list (that’s before the multislider, sneaky fellow :P). Nevertheless, not a bad method at all.

Here’s my try. It’s slower for bigger numbers, but it’s a very different approach:

– Pasted Max Patch, click to expand. –
#173339
Feb 4, 2010 at 4:56pm

Hello Bas van der Graaff

Your remark is very sensible (actually I realized it later, but I was too lazy to modify my post) ; i’ll have a look to yours … tomorrow (it’s time of the beer now) and modify mine accordingly … (if I have the courage).

#173340
Feb 5, 2010 at 1:27pm

Hello Bas van der Graaff,

a efficient mix between mine and yours :

– Pasted Max Patch, click to expand. –
#173341
Feb 8, 2010 at 2:18pm

Another made from scratch. Its a recursive problem as is the solution…

– Pasted Max Patch, click to expand. –
#173342
Feb 11, 2010 at 9:55am

Thanks a lot! I haven’t had time to study them in detail yet, but they all seem to work beautifully.
Thanks again for sharing.

Eskil

#173343
Apr 8, 2010 at 10:02am

hello!

i’ve posted this request before, but it remained “unanswered”, maybe due to a misleading topic title.. this thread clearly suits my intentions, at least it strongly hits the right direction..

what if I’d like to leave pure randomism aside and be able to fix any of the 4 integers and merely let the remaining ones be added/calculated?

for example (sum of 20, list length of 4): the first three integers are fixed (4, 4, 2), the last integer would have to “deliver” a 10 to complete a predefined value of 20. at this point max should “take over”. what would I need? (I’m planing of dealing with way higher values than 20 once I’ve understood the math)

thankful for any hint!

-jonas

#173344

You must be logged in to reply to this topic.