Simple Matlab Random Number Generation

I have to get 5 random numbers a1, a2, a3, a4, a5 where each a1, a2, a3, a4, a5 should be between [-0.5, 0.5] and sum i.e. a1 + a2 + a3 + a4 + a5 = 1.
How should I do it?

4 Kommentare

bym
bym am 27 Feb. 2011
I don't think the problem statement is consistent. There is some probability that you could draw [.5 .5 .5 .5 .5]
Paulo Silva
Paulo Silva am 27 Feb. 2011
Hi Sam, why "Simple Matlab Random Number Generation"? it's not that simple.
Sam Da
Sam Da am 27 Feb. 2011
We have to make sure that such combination as [.5 .5 .5 .5 .5] doesn't happen.
Paulo Silva
Paulo Silva am 28 Feb. 2011
I deleted my answer (the one that was accepted but it wasn't the best one) and voted on Bruno's and Matt's answers.
Please reselect (Sam or someone who can (admins?!)) the best answer, thank you.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

chris hinkle
chris hinkle am 27 Feb. 2011

0 Stimmen

Create 5 arrays that have all possible combinations of these numbers then generate a random number that is between 1 and length of array and then use that value as the index for the array and viola, there's your number. The size of these arrays can be controlled by the resolution you go to.

Weitere Antworten (2)

Bruno Luong
Bruno Luong am 27 Feb. 2011

6 Stimmen

To generate true uniform distribution, the correct method is not quite straightforward. I strongly recommend Roger Stafford's FEX,
http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum

3 Kommentare

Jan
Jan am 27 Feb. 2011
This is defintely the best answer.
the cyclist
the cyclist am 27 Feb. 2011
Agreed that this is the definitive answer. Specifically for Sam's solution:
X = randfixedsum(5,10000,1,-0.5,0.5);
Matt Tearle
Matt Tearle am 27 Feb. 2011
Very nice!

Melden Sie sich an, um zu kommentieren.

Matt Tearle
Matt Tearle am 27 Feb. 2011

1 Stimme

How about a brute-force approach?
ntot = 0;
n = 10000;
x = zeros(n,5);
while ntot<n
r = rand(100,4)-0.5;
r5 = 1 - sum(r,2);
idx = (r5>-0.5) & (r5<0.5);
tmp = [r(idx,:),r5(idx)];
nidx = min(size(tmp,1),n-ntot);
x(ntot+1:ntot+nidx,:) = tmp(1:nidx,:);
ntot = ntot + nidx;
end

1 Kommentar

the cyclist
the cyclist am 28 Feb. 2011
My first reaction to this solution was that, as a rejection method (with a loop, no less!), it would be much slower than Roger's method. The reality is that is does comparably well, speed-wise. I haven't done a full-blown comparison, but I think the reason is two-fold. First, you "semi-vectorized" by pulling chunks of random numbers at a time. Second, and I think more importantly, the accept/reject fraction is pretty good. (It might not be so favorable otherwise, like if the marginals were on [0,1] and still had to sum to 1.)
This solution is highly intuitive, and I believe leads to marginal distributions and correlations between summands that are identical to Roger's solution.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by