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
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:
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...
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:
Another made from scratch. Its a recursive problem as is the solution...
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
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