The problem with the alternative answers given, is they tend to have a flaw, in that they do not sample the space of interest well. It is the same reason why those schemes fail, when you try to sample uniformly, but with a fixed sum. The problem is though, the goal is a difficult one.
First, rather than saying we want to just generically say we want to randomly sample, first, we need to indicate a distribution. My logical initial choice would be an exponential distribution. Thus, can we sample points from an exponential random variable, such that the sum of the set is some fixed value? In fact, even this seems difficult, because an exponential random variable can yield samples that are infinitely large. But, suppose we used the classic (and arguably problematic) approach?
For example, I want to generate a set of M values, such that the sum is N, but the distribution is something based on an exponential random variable?
This means we want to find a random set that lies within a planar region in M dimensions, but is also exponentially distributed? Ugh. So let me try an obvious solution, and see what happens.
numsamples = 10000;
numdim = 2;
lambda = 1;
X = -log(rand(numsamples,numdim))/lambda;
As you should see, the columns of X are (initially) exponentially distributed.
X = X./sum(X,2);
Which looks surprsingly uniformly distributed! (Something I would need to think about.)
My point in all of this, is you need to carefully think about the implications of the re-scaling operation, and what it does to the distribution of the numbers as generated.
Perhaps a better choice might be to use a beta random variable initially. So, in 3-dimensions, with M==3...
X = betarnd(.25,.25,[numsamples,3]);
X = X./sum(X,2);
So there are many ssamples with very small values of each variable, as well as a spike near the top end of the histogram too.
Plotting now in 3-dimensions, we see:
Which is an interesting plot in itself. Again though, the re-sclaing operation does strange, and sometimes nasty things to what you should expect.