Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random numbers

1 view
Skip to first unread message

naila

unread,
Apr 16, 2011, 9:30:21 AM4/16/11
to
Hi,
Im new to Matlab and already having problems!
I dont know how to generate weighted random numbers. For example, three randomly generated values which sum to 1.
Please could you let me know of the coding!?
thanks

Wayne King

unread,
Apr 16, 2011, 9:47:04 AM4/16/11
to
"naila" wrote in message <ioc5ld$bi1$1...@fred.mathworks.com>...

Hi, One thing you can do:

x = rand(3,1);
x = abs(x).^2/norm(x,2)^2;
% sum(x)=1

Of course this will work for random numbers generated for any distribution with finite variance.

x = randn(100,1);
x = abs(x).^2/norm(x,2)^2;

Wayne

John D'Errico

unread,
Apr 16, 2011, 9:53:04 AM4/16/11
to
"naila" wrote in message <ioc5ld$bi1$1...@fred.mathworks.com>...

randfixedsum

As found on the file exchange (written by Roger Stafford.)

Any other scheme that others will offer fails to give a
uniformly distributed set of numbers. This includes the
commonly offered idea of generating a random set and
then scaling them to sum to 1, or translation by subtracting
off the mean.

John

naila

unread,
Apr 16, 2011, 10:34:04 AM4/16/11
to
thanks! Trying to get my head round the software!

naila

unread,
Apr 16, 2011, 10:57:04 AM4/16/11
to
Hi,
Also, when i generate the random number I would need to repeat it a couple of times;

for i=1:4
x = rand(1,3);
x = abs(x).^2/norm(x,2)^2
end

only one set of values appear within the variable editor (spreadsheet) for x appears. Any suggestions how to get all four repeat on the 'spreadsheet' in each row.
thanks

Wayne King

unread,
Apr 16, 2011, 12:07:06 PM4/16/11
to
"naila" wrote in message <iocao0$4r9$1...@fred.mathworks.com>...

Hi, There's nothing to keep you from generating a matrix of random numbers.

x = randn(4,3);
for k = 1:size(x,1)
normx(k) = norm(x(k,:),2)^2;
end
invnormx = (1./normx)';
invnormx = repmat(invnormx,1,3);
x = abs(x).^2;
x = x.*invnormx;
% check sum(x,2)

Wayne

0 new messages