Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: What is the probability that random integers sum to a given value?
Date: Tue, 30 Jun 2009 21:48:01 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 25
Message-ID: <h2e16h$hfr$1@fred.mathworks.com>
References: <h2de1d$sgb$1@fred.mathworks.com> <h2dvi2$sn3$1@fred.mathworks.com> <h2e02t$4bt$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246398481 17915 172.30.248.38 (30 Jun 2009 21:48:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 30 Jun 2009 21:48:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:551850


A slighly cleaner code

m=9; % vector length (w)
n=20; % define n such that 0<=w<=n

% engine
% possible sum vector 
s = (0:n*m);
% Compute probabillity
c = zeros(1,m*(n-1)+1);
i = cumsum([n-m+2 repmat(n+1,1,m-2)]);
c(i) = arrayfun(@(k) (-1)^k*nchoosek(m,k), 1:m-1);
for k=0:m-1
    c = cumsum([nchoosek(m-1,k) c]);
end
P = c / sum(c);

% Graphic
figure;
plot(s, P, '-o');
xlabel('sum');
ylabel('P(sum)');
title(sprintf('vector length = %d, n = %d', m, n));

% Bruno