http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730
MATLAB Central Newsreader  gamma random function
Feed for thread: gamma random function
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 16 Sep 2010 19:29:04 +0000
gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#780083
David
I am trying to create a repeatable gamma random function but i cannot figure out how it's done. i've tried gamrnd and randg but neither seem to work. (I also can't use randg because i need to set the scale param to 2 but it only allows the shape param to vary). I already have repeating sequences for rand, randn, and randi but can't figure out if it's possible with gamrnd. i implemented the following code which is repeatable for the 1st 3 functions. any help is appreciated.<br>
<br>
[s1,s2,s3,s4] = RandStream.create('mrg32k3a','NumStreams',4,'Seed',0);<br>
<br>
rn = randn(s1,10,1); % Normal distributions<br>
ru = rand(s2,10,1); % Uniform distributions<br>
ri = randi(s3,100,10,1); % interger uniform<br>
<br>
rg = gamrnd(2,1,10,1)

Thu, 16 Sep 2010 20:18:19 +0000
Re: gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#780111
Rogelio
"David " <david.j.ellis@saic.com> wrote in message <i6tr60$bqp$1@fred.mathworks.com>...<br>
> I am trying to create a repeatable gamma random function but i cannot figure out how it's done. i've tried gamrnd and randg but neither seem to work. (I also can't use randg because i need to set the scale param to 2 but it only allows the shape param to vary). I already have repeating sequences for rand, randn, and randi but can't figure out if it's possible with gamrnd. i implemented the following code which is repeatable for the 1st 3 functions. any help is appreciated.<br>
> <br>
> [s1,s2,s3,s4] = RandStream.create('mrg32k3a','NumStreams',4,'Seed',0);<br>
> <br>
> rn = randn(s1,10,1); % Normal distributions<br>
> ru = rand(s2,10,1); % Uniform distributions<br>
> ri = randi(s3,100,10,1); % interger uniform<br>
> <br>
> rg = gamrnd(2,1,10,1)<br>
<br>
Hello,<br>
Im not sure if I understand your question.... are you trying to set the seed for the gamma ramdom generator number? so anyone can replicate a code? Or you are experiencing problems with the function gamrnd???

Fri, 17 Sep 2010 00:37:05 +0000
Re: gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#780164
David
"Rogelio " <rogelioa@math.uio.no> wrote in message <i6tu2b$gu1$1@fred.mathworks.com>...<br>
> "David " <david.j.ellis@saic.com> wrote in message <i6tr60$bqp$1@fred.mathworks.com>...<br>
> > I am trying to create a repeatable gamma random function but i cannot figure out how it's done. i've tried gamrnd and randg but neither seem to work. (I also can't use randg because i need to set the scale param to 2 but it only allows the shape param to vary). I already have repeating sequences for rand, randn, and randi but can't figure out if it's possible with gamrnd. i implemented the following code which is repeatable for the 1st 3 functions. any help is appreciated.<br>
> > <br>
> > [s1,s2,s3,s4] = RandStream.create('mrg32k3a','NumStreams',4,'Seed',0);<br>
> > <br>
> > rn = randn(s1,10,1); % Normal distributions<br>
> > ru = rand(s2,10,1); % Uniform distributions<br>
> > ri = randi(s3,100,10,1); % interger uniform<br>
> > <br>
> > rg = gamrnd(2,1,10,1)<br>
> <br>
> Hello,<br>
> Im not sure if I understand your question.... are you trying to set the seed for the gamma ramdom generator number? so anyone can replicate a code? Or you are experiencing problems with the function gamrnd???<br>
<br>
trying to set a starting seed so i can get a reproducible sequence every time thru my code

Fri, 17 Sep 2010 01:40:20 +0000
Re: gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#780169
Roger Stafford
"David " <david.j.ellis@saic.com> wrote in message <i6tr60$bqp$1@fred.mathworks.com>...<br>
> I am trying to create a repeatable gamma random function but i cannot figure out how it's done. i've tried gamrnd and randg but neither seem to work. (I also can't use randg because i need to set the scale param to 2 but it only allows the shape param to vary). I already have repeating sequences for rand, randn, and randi but can't figure out if it's possible with gamrnd. i implemented the following code which is repeatable for the 1st 3 functions. any help is appreciated.<br>
> <br>
> [s1,s2,s3,s4] = RandStream.create('mrg32k3a','NumStreams',4,'Seed',0);<br>
> <br>
> rn = randn(s1,10,1); % Normal distributions<br>
> ru = rand(s2,10,1); % Uniform distributions<br>
> ri = randi(s3,100,10,1); % interger uniform<br>
> <br>
> rg = gamrnd(2,1,10,1)<br>
         <br>
This statement in Mathworks' Statistics Toolbox documentation should indicate what you need to do. "RNGs [random number generators] in Statistics Toolbox software depend on MATLAB's default random number stream via the rand and randn functions, ...".<br>
<br>
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.<br>
<br>
Roger Stafford

Fri, 17 Sep 2010 10:04:05 +0000
Re: gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#780230
Rogelio
> 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.<br>
> <br>
> Roger Stafford<br>
<br>
Thats right. This will give you the same set of numbers each time.<br>
seed=15;<br>
randn('state',seed)<br>
rand('state',seed)<br>
randg(4,3,1)<br>
Just two things, you need to set randn and rand everytime before using randg. And gamrnd calls randg, so this also apply to gamrnd.

Fri, 17 Sep 2010 10:45:06 +0000
Re: gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#780234
Wayne King
"David " <david.j.ellis@saic.com> wrote in message <i6ud7h$qm0$1@fred.mathworks.com>...<br>
> "Rogelio " <rogelioa@math.uio.no> wrote in message <i6tu2b$gu1$1@fred.mathworks.com>...<br>
> > "David " <david.j.ellis@saic.com> wrote in message <i6tr60$bqp$1@fred.mathworks.com>...<br>
> > > I am trying to create a repeatable gamma random function but i cannot figure out how it's done. i've tried gamrnd and randg but neither seem to work. (I also can't use randg because i need to set the scale param to 2 but it only allows the shape param to vary). I already have repeating sequences for rand, randn, and randi but can't figure out if it's possible with gamrnd. i implemented the following code which is repeatable for the 1st 3 functions. any help is appreciated.<br>
> > > <br>
> > > [s1,s2,s3,s4] = RandStream.create('mrg32k3a','NumStreams',4,'Seed',0);<br>
> > > <br>
> > > rn = randn(s1,10,1); % Normal distributions<br>
> > > ru = rand(s2,10,1); % Uniform distributions<br>
> > > ri = randi(s3,100,10,1); % interger uniform<br>
> > > <br>
> > > rg = gamrnd(2,1,10,1)<br>
> > <br>
> > Hello,<br>
> > Im not sure if I understand your question.... are you trying to set the seed for the gamma ramdom generator number? so anyone can replicate a code? Or you are experiencing problems with the function gamrnd???<br>
> <br>
> trying to set a starting seed so i can get a reproducible sequence every time thru my code<br>
<br>
Hi David, Using the Randstream syntax:<br>
<br>
% just an example<br>
rs = RandStream.create('mt19937ar','Seed',2007);<br>
% make your stream the default stream<br>
RandStream.setDefaultStream(rs)<br>
R = gamrnd(2,3,10,1)<br>
% use the reset method<br>
reset(RandStream.getDefaultStream)<br>
R = gamrnd(2,3,10,1)<br>
<br>
Wayne

Fri, 17 Sep 2010 14:12:50 +0000
Re: gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#780282
Steven_Lord
<br>
<br>
"Rogelio " <rogelioa@math.uio.no> wrote in message <br>
news:i6veel$bli$1@fred.mathworks.com...<br>
>> Presumably all you need to do is set some fixed seed you have chosen <br>
>> for either rand or else randn (I don't know which) just prior to <br>
>> generating the sequence of gammarnd quantities to make them repeat the <br>
>> same sequence of values. Of course that presupposes that other differing <br>
>> uses of rand or randn do not intervene during such a generation sequence.<br>
>><br>
>> Roger Stafford<br>
><br>
> Thats right. This will give you the same set of numbers each time.<br>
> seed=15;<br>
> randn('state',seed)<br>
> rand('state',seed)<br>
> randg(4,3,1)<br>
> Just two things, you need to set randn and rand everytime before using <br>
> randg. And gamrnd calls randg, so this also apply to gamrnd.<br>
<br>
But the syntax you posted does a bit more than you think it does.<br>
<br>
<a href="http://www.mathworks.com/help/techdoc/math/bsn94u01.html">http://www.mathworks.com/help/techdoc/math/bsn94u01.html</a><br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
comp.softsys.matlab (CSSM) FAQ: <a href="http://matlabwiki.mathworks.com/MATLAB_FAQ">http://matlabwiki.mathworks.com/MATLAB_FAQ</a><br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Fri, 17 Sep 2010 23:56:00 +0000
Re: gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#780403
Peter Perkins
On 9/17/2010 6:04 AM, Rogelio wrote:<br>
>> Presumably all you need to do is set some fixed seed you have chosen<br>
>> for either rand or else randn (I don't know which) just prior to<br>
>> generating the sequence of gammarnd quantities to make them repeat the<br>
>> same sequence of values. Of course that presupposes that other<br>
>> differing uses of rand or randn do not intervene during such a<br>
>> generation sequence.<br>
>><br>
>> Roger Stafford<br>
><br>
> Thats right. This will give you the same set of numbers each time.<br>
> seed=15;<br>
> randn('state',seed)<br>
> rand('state',seed)<br>
> randg(4,3,1)<br>
> Just two things, you need to set randn and rand everytime before using<br>
> randg. And gamrnd calls randg, so this also apply to gamrnd.<br>
<br>
Unless you are using a version of MATLAB prior to R2008b, I strongly <br>
recommend that you NOT DO THE ABOVE. The syntax above puts you back to <br>
using an old random number generator that is NOT RECOMMENDED.<br>
<br>
Roger has it almost right, except that since R2008b, RAND, RANDN, and <br>
RANDI all depend on one stream of random numbers, as described in their <br>
helps. So if you want to repeat random numbers, ALL YOU NEED TO DO IS:<br>
<br>
s = RandStream.getDefaultStream;<br>
reset(s);<br>
<br>
and then call rand, randn, randi, or any function that depends on them, <br>
which includes all the random number generator functions in the <br>
Statistics Toolbox. You'll get the same thing as if you restarted <br>
MATLAB, assuming that you have not executed a command that changes <br>
MATLAB's global random number stream.<br>
<br>
If you want to repeat random numbers, but not the same ones as you get <br>
at MATLAB startup, then do this:<br>
<br>
s = RandStream.getDefaultStream;<br>
reset(s,1); % or 2, or 3, or 9172010<br>
[call rand or etc.]<br>
reset(s,1);<br>
[cal rand etc.]<br>
<br>
David, you've used a much more complicated scheme where your using a <br>
different stream of random numbers for each distribution. You may have <br>
meant to do that, and it is perfectly valid. If you're really after <br>
something along those lines, I recommend you read these two blog entries:<br>
<br>
<<a href="http://blogs.mathworks.com/loren/2008/11/05/newwayswithrandomnumbersparti/">http://blogs.mathworks.com/loren/2008/11/05/newwayswithrandomnumbersparti/</a>><br>
<<a href="http://blogs.mathworks.com/loren/2008/11/13/newwayswithrandomnumberspartii/">http://blogs.mathworks.com/loren/2008/11/13/newwayswithrandomnumberspartii/</a>><br>
<br>
However, be aware that the RNG functions in the Statistics Toolbox such <br>
as gamrnd do not (yet) support a stream input argument. You'll have to <br>
create your own version for now, such as<br>
<br>
function r = mygamrnd(stream,varargin)<br>
oldStream = RandStream.setDefaultStream(stream);<br>
r = gamrnd(varargin{:});<br>
RandStream.setDefaultStream(oldStream);<br>
<br>
But again, that is perhaps not what you were really wanting to do. Hope <br>
this helps.

Mon, 20 Sep 2010 19:46:05 +0000
Re: gamma random function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291730#781009
David
Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <i70v6g$923$1@fred.mathworks.com>...<br>
> On 9/17/2010 6:04 AM, Rogelio wrote:<br>
> >> Presumably all you need to do is set some fixed seed you have chosen<br>
> >> for either rand or else randn (I don't know which) just prior to<br>
> >> generating the sequence of gammarnd quantities to make them repeat the<br>
> >> same sequence of values. Of course that presupposes that other<br>
> >> differing uses of rand or randn do not intervene during such a<br>
> >> generation sequence.<br>
> >><br>
> >> Roger Stafford<br>
> ><br>
> > Thats right. This will give you the same set of numbers each time.<br>
> > seed=15;<br>
> > randn('state',seed)<br>
> > rand('state',seed)<br>
> > randg(4,3,1)<br>
> > Just two things, you need to set randn and rand everytime before using<br>
> > randg. And gamrnd calls randg, so this also apply to gamrnd.<br>
> <br>
> Unless you are using a version of MATLAB prior to R2008b, I strongly <br>
> recommend that you NOT DO THE ABOVE. The syntax above puts you back to <br>
> using an old random number generator that is NOT RECOMMENDED.<br>
> <br>
> Roger has it almost right, except that since R2008b, RAND, RANDN, and <br>
> RANDI all depend on one stream of random numbers, as described in their <br>
> helps. So if you want to repeat random numbers, ALL YOU NEED TO DO IS:<br>
> <br>
> s = RandStream.getDefaultStream;<br>
> reset(s);<br>
> <br>
> and then call rand, randn, randi, or any function that depends on them, <br>
> which includes all the random number generator functions in the <br>
> Statistics Toolbox. You'll get the same thing as if you restarted <br>
> MATLAB, assuming that you have not executed a command that changes <br>
> MATLAB's global random number stream.<br>
> <br>
> If you want to repeat random numbers, but not the same ones as you get <br>
> at MATLAB startup, then do this:<br>
> <br>
> s = RandStream.getDefaultStream;<br>
> reset(s,1); % or 2, or 3, or 9172010<br>
> [call rand or etc.]<br>
> reset(s,1);<br>
> [cal rand etc.]<br>
> <br>
> David, you've used a much more complicated scheme where your using a <br>
> different stream of random numbers for each distribution. You may have <br>
> meant to do that, and it is perfectly valid. If you're really after <br>
> something along those lines, I recommend you read these two blog entries:<br>
> <br>
> <<a href="http://blogs.mathworks.com/loren/2008/11/05/newwayswithrandomnumbersparti/">http://blogs.mathworks.com/loren/2008/11/05/newwayswithrandomnumbersparti/</a>><br>
> <<a href="http://blogs.mathworks.com/loren/2008/11/13/newwayswithrandomnumberspartii/">http://blogs.mathworks.com/loren/2008/11/13/newwayswithrandomnumberspartii/</a>><br>
> <br>
> However, be aware that the RNG functions in the Statistics Toolbox such <br>
> as gamrnd do not (yet) support a stream input argument. You'll have to <br>
> create your own version for now, such as<br>
> <br>
> function r = mygamrnd(stream,varargin)<br>
> oldStream = RandStream.setDefaultStream(stream);<br>
> r = gamrnd(varargin{:});<br>
> RandStream.setDefaultStream(oldStream);<br>
> <br>
> But again, that is perhaps not what you were really wanting to do. Hope <br>
> this helps.<br>
<br>
Thanks all<br>
I've tried a bunch of these already and I thought i understood the random number initialization but now i think i need to review it a little more. I am 2009b, which is why i've avoided using the randn('state',seed) method as i'm told it'll no longer be supported. I'll study all these comments, and i've also got a help question into mathworks so if i don't figure it out i should be able to get something from them.<br>
thanks again