MATLAB: Generate random number with total sum is 10

sum equal to 1table

How can i generate a table of 6×2 with sum of the row is 10 for example:
0.6 0.4
0.5 0.5
0.7 0.3
i try this way:
r = rand(1, 3); % Start with 3 random numbers that don't sum to 1.
r = r / sum(r) % Normalize so the sum is 1.
however the sum is vertically. How i want to make the sum horizontally?
theSum = sum(r) % Check to make sure. Should be 1
tqvm

Best Answer

For the case of more generic sum of n variables Xi is equal to 1.
If the apriori distribution is supposed to be uniform density
Xi ~ U(0,1)
If the conditioning is
sum(Xi) = 1
we can show that the Xi conditional probability distribution has density of
pdf(Xi | sum Xi = 1) = (n-1)*(1-x)^(n-2).
Note that for n=2, the conditioning distribution happens to be a uniform distribution. This is not true of n > 2.
One neatly way to generate a proper conditioning distribution is using n independant exponential distribution then normalize by the sum
n = 4;
Y = -log(rand(100000,n));
X = Y ./ sum(Y,2); % Each row is a realization of the conditining
We can check it gives the expected conditioning distribution
x=linspace(0,1);
pdf=@(x)(n-1)*(1-x).^(n-2);
histogram(X(:),'Normalization','pdf');
hold on
plot(x,pdf(x),'MarkerSize',3);
title('exponential normamlization');
If you want further to constraint the Xi with in a lower and upper bounds, Roger Stafford's randfixedsum function does the job.