How to generate a random vector with fixed sum and bounded elements

446 Views Asked by At

How we can generate a random vector $E =[e_1, e_2,e_3.\dots, e_N] \in R^N$ such that $\sum_i^N e_i = T \;$ and $ 0 \le e_ i \le d_i$ $\forall i \in 1,2,3,\dots,N$ where $d_i$ specifies the upper bound on each element of the vector. Is this problem over constrained? if yes, any guidance to for relaxation.

1

There are 1 best solutions below

2
On BEST ANSWER

An obvious necessary condition is to have $\sum_{i=1}^N d_i \ge T$.

If you don't care about choosing uniformly, the simplest is to generate $N$ random numbers $0 \le r_i \le d_i$, and compute $R = \sum_{i=1}^N r_i$.

In the case $R<T$, increase the $e_i$ one by one to their maximum until the $\sum_{i=1}^N e_i = T$ as needed. The procedure is guaranteed to work if $\sum_{i=1}^N d_i \ge T$.

Otherwise (if $R > T$), then set $e_i = T r_i/R$. Then, by construction, $$ \sum_{i=1}^n e_i = \sum_{i=1}^n \frac{T r_i}{R} = \frac{T}{R} \sum_{i=1}^n r_i = T. $$