Generate N random numbers within a range with a constant sum

前端 未结 5 827
暗喜
暗喜 2020-12-03 12:26

I want to generate N random numbers drawn from a specif distribution (e.g uniform random) between [a,b] which sum to a constant C. I have tried a couple of solutions I could

5条回答
  •  有刺的猬
    2020-12-03 12:45

    Let's try to simplify the problem. By substracting the lower bound, we can reduce it to finding N numbers in [0,b-a] such that their sum is C-Na.

    Renaming the parameters, we can look for N numbers in [0,m] whose sum is S.

    Now the problem is akin to partitioning a segment of length S in N distinct sub-segments of length [0,m].

    I think the problem is simply not solvable.

    if S=1, N=1000 and m anything above 0, the only possible repartition is one 1 and 999 zeroes, which is nothing like a random spread.

    There is a correlation between N, m and S, and even picking random values will not make it disappear.

    For the most uniform repartition, the length of the sub-segments will follow a gaussian curve with a mean value of S/N.

    If you tweak your random numbers differently, you will end up with whatever bias, but in the end you will never have both a uniform [a,b] repartition and a total length of C, unless the length of your [a,b] interval happens to be 2C/N-a.

提交回复
热议问题