Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Random number choice according to probability distribution
Date: Wed, 7 Mar 2012 11:54:20 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 31
Message-ID: <jj7i9c$l0s$1@newscl01ah.mathworks.com>
References: <jj5t0t$d5f$1@newscl01ah.mathworks.com> <jj62ua$432$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-06-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1331121260 21532 172.30.248.38 (7 Mar 2012 11:54:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 7 Mar 2012 11:54:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 3364997
Xref: news.mathworks.com comp.soft-sys.matlab:760179

Hi Roger,
I've already tried this, following your sugestion from an older message in this forum. My code is like this:

c = cumsum(p);
r = rand(1,1);
e = [0,c];
[~,bin] = histc(r,e);
x = s(bin);

But running the routine several times, I noticed that sometimes the distribution of the random choices doesn't follow the probability distribution. For example, sometimes the 2nd or the 3rd values are chosen more frequently than the 1st value of vector 's'. I'm expecting something like

nc_s(1) > nc_s(2) > nc_s(3) > ... > nc_s(19)

and instead of this, sometimes I have an output like

nc_s(2) > nc_s(1) > nc_s(3) > ... > nc_s(19)

(where nc_p(k) represents how many times the k-th value from vector 's' has ben chosen).
Am I doing something wrong?

Thanks.



"Roger Stafford" wrote in message <jj62ua$432$1@newscl01ah.mathworks.com>...
> - - - - - - - - - - 
>   Use [0,cumsum(p)] as the "edges" (2nd input) in 'histc' with rand(1,n) as first input.  (Fudge the last edge value up by a few 'eps' to avoid getting the final 'edge'.)  Then the 2nd output ('bin') can be used as n random indices into s.
> 
>   Be sure to read the documentation for 'histc' carefully to get it right.
> 
> Roger Stafford