Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: Generation of random number from Define probability Date: Thu, 19 Feb 2009 06:52:02 +0000 (UTC) Organization: Battelle Energy Alliance (INL) Lines: 21 Message-ID: <gnivii$de5$1@fred.mathworks.com> References: <gnepo2$18j$1@fred.mathworks.com> <gnf7ru$fg5$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-02-blr.mathworks.com Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1235026322 13765 172.30.248.37 (19 Feb 2009 06:52:02 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Thu, 19 Feb 2009 06:52:02 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 688530 Xref: news.mathworks.com comp.soft-sys.matlab:519329 "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message Please forgive me if this is totally wrong, (I admit I hated probability and statistics back in college) but why wouldn't something simple like this work: p = [.679 .179 .129 .013]'; p = p/sum(p); % They must have a sum of one n = 20000000; [ps,idx] = sort(p); ps = cumsum(ps); x = rand(n,1); E = x; E(x<=ps(1)) = idx(1); for ii = 2:length(p) E(x>=ps(ii-1) & x<=ps(ii) ) = idx(ii); end It seems pretty fast for large n, and I don't see the reason why this wouldn't work. h]LJJRJhPKhXoY^QK]JoVJQXbhNNLJhWY]6_.hXXNhUT)QvJB#Rh^UNXVWX