MATLAB: How to generate uniformly distributed weights between intervals that sum to one

random number generatoruniformweight

Hi, I have three weight intervals: w1=[0.25,0.50] w2=[0.15,0.40] w3=[0.20,0.60]
How can I generate 1000 random weight vectors (uniform distribution) Weight_vector=(w1,w2,w3) that each Wi correspond to related interval and they sum to one? Two examples:
Weight_vector1: (0.25,0.25,0.50);
Weight_vector2: (0.30,0.15,0.55);
Thanks, Hossein

Best Answer

For general sets of intervals, randfixedsum does not work. For this reason I provided randFixedLinearCombination . It handles a much more general problem than randfixedsum, although for large numbers of variables, it will not be as efficient as randfixedsum.
n = 10;
lb = [.25 .15 .20];
ub = [.5 .4 .6];
linearCombination = [1 1 1];
rhs = 1;
xyz = randFixedLinearCombination(n,rhs,linearCombination,lb,ub)
xyz =
0.276646000258171 0.397179072324999 0.32617492741683
0.479429538804855 0.25335926183292 0.267211199362225
0.436921197465117 0.310210767126349 0.252868035408533
0.473906980805434 0.168956179849817 0.357136839344749
0.397453211716306 0.257931038823466 0.344615749460228
0.370300633544778 0.3798772062265 0.249822160228722
0.291816855265393 0.393117202614066 0.315065942120541
0.355210364802235 0.336898845478376 0.307890789719389
0.453532562192296 0.261548824228386 0.284918613579318
0.300587077933784 0.256205660055041 0.443207262011175
sum(xyz,2)
ans =
1
1
1
1
1
1
1
1
1
1
As you can see, each row sums to 1. Each column lies within the lower and upper bounds for that variable.