Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: faster implementation of discrete random number generator? Date: Mon, 25 May 2009 02:12:01 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 27 Message-ID: <gvcuph$dvv$1@fred.mathworks.com> References: <gvclbk$39o$1@fred.mathworks.com> Reply-To: <HIDDEN> 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 1243217521 14335 172.30.248.38 (25 May 2009 02:12:01 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Mon, 25 May 2009 02:12:01 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1826773 Xref: news.mathworks.com comp.soft-sys.matlab:542258 Thank you very much. I hope your function will be useful.. actually my N is fairly small but I call the function quite a bit so we'll see... As for my discreternd function I believe it is the same one you found on Google but with some modifications I have made to it. I should probably credit the original author but did not really mean to post it publicly or anything I just noticed its where most time is spent. Thank you again Misha "Misha Koshelev" <mk144210@bcm.edu> wrote in message <gvclbk$39o$1@fred.mathworks.com>... > Seem like this is where most time is spent according to profiler: > > function samples = discreternd(p,N) > % DISCRETERND Sample from a discrete distribution. > % SAMPLES = DISCRETERND(p,N) Return N elements from the discrete > % distribution with individual probabilities p. > % > > sum = [0 cumsum(p)]; > sum = sum ./ max(sum); > > samples = zeros(1,N); > u = rand(N,1); > for i=1:length(sum)-1 > samples((u > sum(i)) & (u <= sum(i+1))) = i; > end > > Thank you > Misha