Hello, quite simple but I would like to generate an array from 3 variables, which consist of a randomised serie of Integers and Floating numbers, that can be summed in a determined result :
For example :
Expected summed result : 7
Numbers of integers : 2
Numbers of floating numbers : 2
Result = [2, 4, 0.5, 0.5] or [3, 2, 1.8, 0.2] etc…

That’s a surprisingly hard problem to solve. The way to think about it is in terms of constraints. For the first number what are the range of numbers that you can generate, and still be able to meet the other constraints.

So first of all select randomly (based upon your distribution) your first type of number. In your case above that’s a 1/2 chance of either floating point, or integer.

Let’s say you pick an integer. For this to work the maximum possible value is 5 (because that would allow 1, and then two floating points that sum to 1,0), and the minimum is 1. Similarly if you chose a float then the number could be anything between 0.01 and 5.999 (depending upon what the smallest floating point number you support is).

So let’s say you had: 5 0.4 1 - then the last number has to be 0.6 (which is easy).
Similarly, let’s say you had 3 0.4 and now pick a float. You can only choose 0.6 1.6 or 2.6 (as the remaining number has to be an integer).

Here’s one way to do it (it won’t find solutions with integer values > (sum div #integers) ),
but an additional step at the end where you iteratively increase some found integer while simultaneously decrease another number in the list can solve that too. (I’ll leave that as an excercise for the interested reader )

(
// user definable
var expected_sum = 7;
var no_of_int = 2;
var no_of_float = 2;
// internal
var max_int_val = expected_sum.div(no_of_int);
var result_int = no_of_int.collect({
| idx |
max_int_val.rrand(1);
});
var sum_so_far = result_int.sum;
var difference_with_expected = expected_sum - sum_so_far;
var result_float = no_of_float.collect({
1.0.rrand(0.0);
}).normalizeSum * difference_with_expected;
var total_result = (result_int ++ result_float).scramble;
total_result.debug("result = ");
total_result.sum.debug("sum reached = ");
)

Basically, I first choose integers, then calculate the floats that fit the gap that still needs to be filled.

Though I think that might distort the distribution in ways that would be less than ideal. Your floating points will be on average smaller using that method, and your integers will be on average bigger.

The one thing here is that you might end up with integers == 0 ?
The original problem didn’t explicitly forbid that but it might be something to keep in mind.
(I’m aware that my solution might also end up with zeros if the desired sum is too small for the wanted #integers).

These solutions are clever and creative, but I’m fairly sure they will distort the probability distribution. Whether this matters probably depends upon the application that it will be used for.

Thanks kindly to all, this community has not stolen his reputation to be patient and helpful. The application is to sequence sounds, all separated by little blocks of silence. I wanted to see what kind of hypnose could produce a sequencing that would keep a regular macro timing (sum 7) but, with inside, a perpetual evolution of the timing of the sustained sound (integers) as well as the timing of the silence that separates them (floating)