Path: news.mathworks.com!not-for-mail From: Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> Newsgroups: comp.soft-sys.matlab Subject: Re: gamma random function Date: Fri, 17 Sep 2010 19:56:00 -0400 Organization: The MathWorks, Inc. Lines: 63 Message-ID: <i70v6g$923$1@fred.mathworks.com> References: <i6tr60$bqp$1@fred.mathworks.com> <i6ugu4$lup$1@fred.mathworks.com> <i6veel$bli$1@fred.mathworks.com> NNTP-Posting-Host: perkinsp.dhcp.mathworks.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: fred.mathworks.com 1284767760 9283 172.31.57.40 (17 Sep 2010 23:56:00 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Fri, 17 Sep 2010 23:56:00 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100802 Thunderbird/3.1.2 In-Reply-To: <i6veel$bli$1@fred.mathworks.com> Xref: news.mathworks.com comp.soft-sys.matlab:671023 On 9/17/2010 6:04 AM, Rogelio wrote: >> Presumably all you need to do is set some fixed seed you have chosen >> for either rand or else randn (I don't know which) just prior to >> generating the sequence of gammarnd quantities to make them repeat the >> same sequence of values. Of course that presupposes that other >> differing uses of rand or randn do not intervene during such a >> generation sequence. >> >> Roger Stafford > > Thats right. This will give you the same set of numbers each time. > seed=15; > randn('state',seed) > rand('state',seed) > randg(4,3,1) > Just two things, you need to set randn and rand everytime before using > randg. And gamrnd calls randg, so this also apply to gamrnd. Unless you are using a version of MATLAB prior to R2008b, I strongly recommend that you NOT DO THE ABOVE. The syntax above puts you back to using an old random number generator that is NOT RECOMMENDED. Roger has it almost right, except that since R2008b, RAND, RANDN, and RANDI all depend on one stream of random numbers, as described in their helps. So if you want to repeat random numbers, ALL YOU NEED TO DO IS: s = RandStream.getDefaultStream; reset(s); and then call rand, randn, randi, or any function that depends on them, which includes all the random number generator functions in the Statistics Toolbox. You'll get the same thing as if you restarted MATLAB, assuming that you have not executed a command that changes MATLAB's global random number stream. If you want to repeat random numbers, but not the same ones as you get at MATLAB startup, then do this: s = RandStream.getDefaultStream; reset(s,1); % or 2, or 3, or 9172010 [call rand or etc.] reset(s,1); [cal rand etc.] David, you've used a much more complicated scheme where your using a different stream of random numbers for each distribution. You may have meant to do that, and it is perfectly valid. If you're really after something along those lines, I recommend you read these two blog entries: <http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/> <http://blogs.mathworks.com/loren/2008/11/13/new-ways-with-random-numbers-part-ii/> However, be aware that the RNG functions in the Statistics Toolbox such as gamrnd do not (yet) support a stream input argument. You'll have to create your own version for now, such as function r = mygamrnd(stream,varargin) oldStream = RandStream.setDefaultStream(stream); r = gamrnd(varargin{:}); RandStream.setDefaultStream(oldStream); But again, that is perhaps not what you were really wanting to do. Hope this helps.