From: Peter Perkins <>
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$>
References: <i6tr60$bqp$> <i6ugu4$lup$> <i6veel$bli$>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: 1284767760 9283 (17 Sep 2010 23:56:00 GMT)
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: Gecko/20100802 Thunderbird/3.1.2
In-Reply-To: <i6veel$bli$>
Xref: 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;

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.]
[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:


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{:});

But again, that is perhaps not what you were really wanting to do.  Hope 
this helps.