I hope I can explain this well, if I don’t I’ll try again.
I want to generate an array of 5 random numbers that all add up to 10 but whose allocation are chosen on an interval of [0,2n/m].
I’m using numpy.
The code I have so far looks like this:
import numpy as np
n=10
m=5
#interval that numbers are generated on
randNumbers= np.random.uniform(0,np.divide(np.multiply(2.0,n),fronts),fronts)
#Here I normalize the random numbers
normNumbers = np.divide(randNumbers,np.sum(randNumbers))
#Next I multiply the normalized numbers by n
newList = np.multiply(normNumbers,n)
#Round the numbers two whole numbers
finalList = np.around(newList)
This works for the most part, however the rounding is off, it will add up to 9 or 11 as opposed to 10. Is there a way to do what I’m trying to do without worrying about rounding errors, or maybe a way to work around them? If you would like for me to be more clear I can, because I have trouble explaining what I’m trying to do with this when talking :).
This generates all the possible combinations that sum to 10 and selects a random one
There may be a more efficient way, but this will select fairly between the outcomes
Lets see how this works when n=10 and m=5
2*n/m+1 = 5, so the expression becomes`*[range(5)]*5 is using argument unpacking. This is equivalent to
product()gives the cartesian product of the parameters, which in this case has 5**5 elements, but we then filter out the ones that don’t add to 10, which leaves a list of 381 valueschoice()is used to select a random value from the resultant list