Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gamma random function

57 views
Skip to first unread message

David

unread,
Sep 16, 2010, 3:29:04 PM9/16/10
to
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.

[s1,s2,s3,s4] = RandStream.create('mrg32k3a','NumStreams',4,'Seed',0);

rn = randn(s1,10,1); % Normal distributions
ru = rand(s2,10,1); % Uniform distributions
ri = randi(s3,100,10,1); % interger uniform

rg = gamrnd(2,1,10,1)

Rogelio

unread,
Sep 16, 2010, 4:18:19 PM9/16/10
to
"David " <david....@saic.com> wrote in message <i6tr60$bqp$1...@fred.mathworks.com>...

Hello,
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???

David

unread,
Sep 16, 2010, 8:37:05 PM9/16/10
to
"Rogelio " <roge...@math.uio.no> wrote in message <i6tu2b$gu1$1...@fred.mathworks.com>...

trying to set a starting seed so i can get a reproducible sequence every time thru my code

Roger Stafford

unread,
Sep 16, 2010, 9:40:20 PM9/16/10
to
"David " <david....@saic.com> wrote in message <i6tr60$bqp$1...@fred.mathworks.com>...
- - - - - - - - - -
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, ...".

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

Rogelio

unread,
Sep 17, 2010, 6:04:05 AM9/17/10
to
> 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.

Wayne King

unread,
Sep 17, 2010, 6:45:06 AM9/17/10
to
"David " <david....@saic.com> wrote in message <i6ud7h$qm0$1...@fred.mathworks.com>...

Hi David, Using the Randstream syntax:

% just an example
rs = RandStream.create('mt19937ar','Seed',2007);
% make your stream the default stream
RandStream.setDefaultStream(rs)
R = gamrnd(2,3,10,1)
% use the reset method
reset(RandStream.getDefaultStream)
R = gamrnd(2,3,10,1)

Wayne

Steven_Lord

unread,
Sep 17, 2010, 10:12:50 AM9/17/10
to

"Rogelio " <roge...@math.uio.no> wrote in message

news:i6veel$bli$1...@fred.mathworks.com...

But the syntax you posted does a bit more than you think it does.

http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Peter Perkins

unread,
Sep 17, 2010, 7:56:00 PM9/17/10
to

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.

David

unread,
Sep 20, 2010, 3:46:05 PM9/20/10
to
Peter Perkins <Peter....@MathRemoveThisWorks.com> wrote in message <i70v6g$923$1...@fred.mathworks.com>...

Thanks all
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.
thanks again

0 new messages