Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
gamma random function

Subject: gamma random function

From: David

Date: 16 Sep, 2010 19:29:04

Message: 1 of 9

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)

Subject: gamma random function

From: Rogelio

Date: 16 Sep, 2010 20:18:19

Message: 2 of 9

"David " <david.j.ellis@saic.com> wrote in message <i6tr60$bqp$1@fred.mathworks.com>...
> 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)

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

Subject: gamma random function

From: David

Date: 17 Sep, 2010 00:37:05

Message: 3 of 9

"Rogelio " <rogelioa@math.uio.no> wrote in message <i6tu2b$gu1$1@fred.mathworks.com>...
> "David " <david.j.ellis@saic.com> wrote in message <i6tr60$bqp$1@fred.mathworks.com>...
> > 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)
>
> 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???

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

Subject: gamma random function

From: Roger Stafford

Date: 17 Sep, 2010 01:40:20

Message: 4 of 9

"David " <david.j.ellis@saic.com> wrote in message <i6tr60$bqp$1@fred.mathworks.com>...
> 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)
- - - - - - - - - -
  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

Subject: gamma random function

From: Rogelio

Date: 17 Sep, 2010 10:04:05

Message: 5 of 9

> 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.

Subject: gamma random function

From: Wayne King

Date: 17 Sep, 2010 10:45:06

Message: 6 of 9

"David " <david.j.ellis@saic.com> wrote in message <i6ud7h$qm0$1@fred.mathworks.com>...
> "Rogelio " <rogelioa@math.uio.no> wrote in message <i6tu2b$gu1$1@fred.mathworks.com>...
> > "David " <david.j.ellis@saic.com> wrote in message <i6tr60$bqp$1@fred.mathworks.com>...
> > > 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)
> >
> > 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???
>
> trying to set a starting seed so i can get a reproducible sequence every time thru my code

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

Subject: gamma random function

From: Steven_Lord

Date: 17 Sep, 2010 14:12:50

Message: 7 of 9



"Rogelio " <rogelioa@math.uio.no> wrote in message
news:i6veel$bli$1@fred.mathworks.com...
>> 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.

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
slord@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

Subject: gamma random function

From: Peter Perkins

Date: 17 Sep, 2010 23:56:00

Message: 8 of 9

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.

Subject: gamma random function

From: David

Date: 20 Sep, 2010 19:46:05

Message: 9 of 9

Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <i70v6g$923$1@fred.mathworks.com>...
> 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.

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us