# Generate random numbers whose sum is equal to a constant value modulo n

I want to generate $n$ random numbers $u_i \in [ 0, 2^{\kappa + 1})$ such that $\sum u_i = c$ $mod$ $2^{\kappa + 1}$, where c is a constant.
Also, taking $n - 1$ random numbers and subtracting their sum from $b \cdotp 2^{\kappa + 1} + c$, where $b \cdotp 2^{\kappa + 1}$ is the closest multiple of $\;2^{\kappa + 1}$ that is greater than sum of the $n - 1$ random numbers, to get the $n^{th}$ random number a good solution?

P.S. I want to know if there's a solution to this problem that'll ensure good statistical properties.

• And the summation is the only criteria for u's distribution? May 20, 2017 at 12:57
• Yes, that's the only criteria. Although, I'd like to know if I can get uniformly distributed random numbers that satisfy this condition. May 20, 2017 at 13:27
• So my answer was wrong, and I don't know of a way to prove that extending the simplex idea to modular arithmetic. I'll delete it in a moment. I do think your proposed idea is a good one, but this is just speculation. The top answer here may be useful.
– user47922
May 20, 2017 at 20:25

For $i=1, \dots, n-1$, generate $n-1$ random numbers $u_i \in [0,2^{κ+1})$ and define $u_n = \left(c - \sum_{i=1}^{n-1} u_i\right) \bmod 2^{κ+1}$.
• That's optimal. When $n>1$, it is easily proven that each $u_i$ generated is uniformly random in $[0,2^{κ+1})$: that's by construction for $u_i$ with $i<n$, and for $u_n$ that follows from the fact that $u_{n-1}$ is uniformly random in $[0,2^{κ+1})$, and$$u_n=\left(c - \sum_{i=1}^{n-2} u_i\right)-u_{n-1}\bmod2^{κ+1}$$