I\'m either looking for an algorithm or a suggestion to improve my code to generate a list of random numbers that their sum equals some arbitrary number. With my code below
All right, we're going to tackle the problem assuming the requirement is to generate a random vector of length N that is uniformly distributed over the allowed space, restated as follows:
Given
generate a random vector V of length N such that the random variable V is uniformly distributed throughout its permitted space.
We can simplify the problem by noting that we can calculate V = U * S where U is a similar random vector with desired total sum 1, and a range of allowed values [0,b] where b = B/S. The value b must be between 1/N and 1.
First consider N = 3. The space of allowed values {U} is a portion of a plane perpendicular to the vector [1 1 1] that passes through the point [1/3 1/3 1/3] and which lies inside the cube whose components range between 0 and b. This set of points {U} is shaped like a hexagon.
(TBD: picture. I can't generate one right now, I need access to MATLAB or another program that can do 3D plots. My installation of Octave can't.)
It is best to use an orthonormal weighting matrix W (see my other answer) with one vector = [1 1 1]/sqrt(3). One such matrix is
octave-3.2.3:1> A=1/sqrt(3)
A = 0.57735
octave-3.2.3:2> K=1/sqrt(3)/(sqrt(3)-1)
K = 0.78868
octave-3.2.3:3> W = [A A A; A 1-K -K; A -K 1-K]
W =
0.57735 0.57735 0.57735
0.57735 0.21132 -0.78868
0.57735 -0.78868 0.21132
which, again, is orthonormal (W*W = I)
If you consider the points of the cube [0 0 b],[0 b b],[0 b 0],[b b 0],[b 0 0], and [b 0 b] these form a hexagon and are all a distance of b*sqrt(2/3) from the cube's diagonal. These do not satisfy the problem in question, but are useful in a minute. The other two points [0 0 0] and [b b b] are on the cube's diagonal.
The orthonormal weighting matrix W allows us to generate points that are uniformly distributed within {U}, because orthonormal matrices are coordinate transformations that rotate/reflect and do not scale or skew.
We will generate points that are uniformly distributed in the coordinate system defined by the 3 vectors of W. The first component is the axis of the diagonal of the cube. The sum of U's components depends completely upon this axis and not at all on the others. Therefore the coordinate along this axis is forced to be 1/sqrt(3) which corresponds to the point [1/3, 1/3, 1/3].
The other two components are in directions perpendicular to the cube's diagonal. Since the maximum distance from the diagonal is b*sqrt(2/3), we will generate uniformly distributed numbers (u,v) between -b*sqrt(2/3) and +b*sqrt(2/3).
This gives us a random variable U' = [1/sqrt(3) u v]. We then compute U = U' * W. Some of the resulting points will be outside the allowable range (each component of U must be between 0 and b), in which case we reject that and start over.
In other words:
The solution is similar for higher dimensions (uniformly distributed points within a portion of the hyperplane perpendicular to a hypercube's main diagonal):
Precalculate a weighting matrix W of rank N.
The range k(N) is a function of N that represents the maximum distance of the vertices of a hypercube of side 1 from its main diagonal. I'm not sure of the general formula but it's sqrt(2/3) for N = 3, sqrt(6/5) for N = 5, there's probably a formula for it somewhere.