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.
Thanks a bunch
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:
----------begin_max5_patcher---------- 984.3oc0YksbaBCE8Y6uBM905lQBs.z252QmLcv1pNJCHx.xsYYx+dERv3kJ rkqAExClEAHNbz4dtWI+17YKVU9LudA3afe.lM6s4ylYZpogYsmOaQQ1yqyy pM21BI+OkqdbwR6kT7mUll+B.00lXioE8c8UZRWi+pTpjYEbyk9dkHKu6Jxc Ek6T4bko+QssZaR8xSbK3VHj5Wz8Gza0hWMWCAuCtuqDxtdJpswmxTqePH29 yJ9ZksyPvD8y.hvwM6vQ2QWBP5l.227HuOedylk2Fg7ZN3wRgzEoPuVRIpOR YwR8ughUXPCa.IlcrlsCMoHbQGjIoFwxFQInwShn.4fUM+bwJnqkUHmSjrJS t0HV5N9pnIT+zDwpSRMhFZ5HEIIUO3hifSy.IKmfiLlLT3XDH41tkjNICkRw idnTuZDRxzTizxIsdtjaRiTrKWIpyEa3UtXf3y+cVyUZHq6PKvr1dvFnr+F5 XAnebiEKqKyKq55TFhxRXK0GQoQoDTyQIQwDp9HzwurZ0K47ircp30boJSIJ kG4PaDTjDC6Aa2rumZGbHWoiF1JSSMQuHJ0rigFb851pxcO4Z7JZZpXQvHKi X1QFk5CT.nKFA8wYqgtLgfsgLQiAgr6UWkLQt5De39HjtBC51OrkYa4DX73o XxKy1TvqqADG7Dd3l9wvoZHFRgZLsXihnQatTw25hOXSSqkDqzH1jSDSusjg ZwP1V9+PJP.DfbZufoePpjyUBoclowGVBIKTpD7zTkzlX1xF2nJoGNoJStor voWBJdRuTFHSHDEGtjPH1mhjPsocrIoGqpVDNmCOZ3VnmFFYjl5NjMpSKysG ChLkm59vjIZcYQAuYb6DR42Y5uxU4bWzxUW6O77e28L0IhckOQVWi+YpSdqf ZWFUSnEYTHqbQsBjykacN6dDZf4K+qhis+S+Vcc2Urx4L2S+DXeDyFhE4pOF HdRy.skxeVJv7tzpX4o+KKl9qo8i4k5xcUq6.cWZVcgqcPdCuVIjl34CtolR 1.6ieePrYCWdnbuPr4oRMGzBBV5AKtAAZr9aNomQOuwJ8HX3FqlOnq.q2JlX 9fIRPwTyh6CvWBSwgGSQW.SMqlZHG6HdL1Y.df4oKEOR+.zSWjmfgESTewDJ bXB5qW5DCSl.g.hoDeF6lfXx3OENLchEcO7TX8Lod4YRCJlZ9eI8CSgari3k dJMn7zIkw0ClhmdXBG3wtHevDKrdATexsD1ZewdW6a33IerLIgsTEhOofOQy M5XxmR5BMlf9fovVNN1Ga7v5X5SxNTXS.6SPmoXl+We.8IuO+ufHwFJ0 -----------end_max5_patcher-----------
Another made from scratch. Its a recursive problem as is the solution…
----------begin_max5_patcher---------- 643.3ocyWEraiBCD8L4qvKW2zHrASf82Xk1KqpVAA2TWA1QfQMaq5+9hGSRS yFHFEBIWLZFaK+lmey3g2m43lJ2xpbQ+.8ajiy6ybb.WZGNs1NtEIaWkmTAK yUvdUl9h6byTJ1VE3NWljUvppPULEh3sadQcgrVkyTvlwsdMtT+cCybxttnG amZShZ0ybw5+TxVoLyhCiV3MGQVpGwddfQy398vy.Hz.qGvK2cxOIEJQRAbB t+hUlkHRN.UbwwfRugJ9avFv3EdZueLaldXtkbil.RVyF2fOFh2Xe8mPBvAQ cD7gCN3IiWv2gvPgRQbD+zbheWbRZhXs6bjKWn18oWVJHPSLQg.IE1qBI3Vp P5fjpX4nNxYHmihrgXHD3i+xdYFxcn7Ii8DqLW95nlSYHEbLTWgF0KofuCkK e6gARGVl+fWhgDH5BZOLh2cnL4sbz5RY8lAlCc17GBFp3RL0Tnz9jJw2g7x2 Q3qjTI.jJ989ZbzsjQRqUJoXfQOTT0lBp6eio6ne4oqNLdWtkIhLYwU59EaT 798Egza486JYQAS2dvQrxOq6fR75IM2zUI07JYPew7ss8g5hTV4.Kx01CksR aSQNZuRax0fDfCxMmKN9GR.rp8+UloRVWtZWPtq4ezm3MiUo3hDEuoJvmKh7 k07LOKiINTcTvy1HanrVLfd7jWS1BIhEHR2Q5DBoXafTyeRfvSFjv1PSwCik vMktzcwPM++DAu25BAazUCq3P5AXErtPrd+kP.W03QNiP22Jb6ZJZg8+eqHn ckVhVabowgFhjyEGdSaZTfEbKc5gjmEPZBUfXaJ.NwYEd1fI7jhIapycDtu1 PhZAjht.D0X7wr+AqfsyRB -----------end_max5_patcher-----------
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.
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!
Forums > MaxMSP