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:
How to generate the same pseudo random sequence ?

Subject: How to generate the same pseudo random sequence ?

From: scaramanga

Date: 13 Jan, 2011 21:23:05

Message: 1 of 6

Hello,

I'm trying to generate the same pseudo ramdom sequence at each computation of my algorithm, then i do the following

stream = RandStream('mt19937ar','Seed',200); % Seed :200
s=rand(5,1);

At each time i evaluate s it's different, do you have any idea ?

Thank you

Subject: How to generate the same pseudo random sequence ?

From: Andy

Date: 13 Jan, 2011 21:32:10

Message: 2 of 6

"scaramanga" wrote in message <ignqfo$f66$1@fred.mathworks.com>...
> Hello,
>
> I'm trying to generate the same pseudo ramdom sequence at each computation of my algorithm, then i do the following
>
> stream = RandStream('mt19937ar','Seed',200); % Seed :200
> s=rand(5,1);
>
> At each time i evaluate s it's different, do you have any idea ?
>
> Thank you

You need to use the stream you're creating:


stream = RandStream('mt19937ar','Seed',200); % Seed :200
s=rand(stream,5,1); % <- pass stream as an argument to rand

Subject: How to generate the same pseudo random sequence ?

From: scaramanga

Date: 13 Jan, 2011 21:39:05

Message: 3 of 6

"Andy" wrote in message <ignr0q$jmj$1@fred.mathworks.com>...
> "scaramanga" wrote in message <ignqfo$f66$1@fred.mathworks.com>...
> > Hello,
> >
> > I'm trying to generate the same pseudo ramdom sequence at each computation of my algorithm, then i do the following
> >
> > stream = RandStream('mt19937ar','Seed',200); % Seed :200
> > s=rand(5,1);
> >
> > At each time i evaluate s it's different, do you have any idea ?
> >
> > Thank you
>
> You need to use the stream you're creating:
>
>

stream = RandStream('mt19937ar','Seed',200); % Seed :200
s=rand(stream,5,1); % <- pass stream as an argument to rand

Actually this is what I'm doing (that was a typo I'm sorry) and I don't get the same results.

Subject: How to generate the same pseudo random sequence ?

From: By learning you will teach; by teaching you will learn.

Date: 13 Jan, 2011 21:39:51

Message: 4 of 6

On 11-01-13 03:39 PM, scaramanga wrote:

> stream = RandStream('mt19937ar','Seed',200); % Seed :200
> s=rand(stream,5,1); % <- pass stream as an argument to rand
>
> Actually this is what I'm doing (that was a typo I'm sorry) and I don't get
> the same results.

Works fine for me if I re-execute both lines of code.

Subject: How to generate the same pseudo random sequence ?

From: Steven_Lord

Date: 13 Jan, 2011 22:08:42

Message: 5 of 6



"scaramanga " <faytstrife@hotmail.fr> wrote in message
news:ignrdp$g0c$1@fred.mathworks.com...
> "Andy" wrote in message <ignr0q$jmj$1@fred.mathworks.com>...
>> "scaramanga" wrote in message <ignqfo$f66$1@fred.mathworks.com>...
>> > Hello,
>> >
>> > I'm trying to generate the same pseudo ramdom sequence at each
>> > computation of my algorithm, then i do the following stream =
>> > RandStream('mt19937ar','Seed',200); % Seed :200
>> > s=rand(5,1);
>> >
>> > At each time i evaluate s it's different, do you have any idea ?
>> >
>> > Thank you
>>
>> You need to use the stream you're creating:
>>
>>
>
> stream = RandStream('mt19937ar','Seed',200); % Seed :200
> s=rand(stream,5,1); % <- pass stream as an argument to rand
>
> Actually this is what I'm doing (that was a typo I'm sorry) and I don't
> get the same results.

Are you doing something like:

stream = RandStream('mt19937ar','Seed',200); % Seed :200
s1=rand(stream,5,1); % <- pass stream as an argument to rand
s2=rand(stream,5,1); % <- pass stream as an argument to rand

and finding that s1 and s2 are different? That's expected -- the first call
to RAND (to generate s1) uses some of the numbers from the stream, so when
you make the second call to RAND (to generate s2) you're at a different
point in the stream. In essence, all the random number functions cause you
to go downstream ;)

If you RESET stream before the second call to RAND, are s1 and s2 equal?

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

Subject: How to generate the same pseudo random sequence ?

From: scaramanga

Date: 14 Jan, 2011 07:57:04

Message: 6 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <ignt5a$aai$1@fred.mathworks.com>...
>
>
> "scaramanga " <faytstrife@hotmail.fr> wrote in message
> news:ignrdp$g0c$1@fred.mathworks.com...
> > "Andy" wrote in message <ignr0q$jmj$1@fred.mathworks.com>...
> >> "scaramanga" wrote in message <ignqfo$f66$1@fred.mathworks.com>...
> >> > Hello,
> >> >
> >> > I'm trying to generate the same pseudo ramdom sequence at each
> >> > computation of my algorithm, then i do the following stream =
> >> > RandStream('mt19937ar','Seed',200); % Seed :200
> >> > s=rand(5,1);
> >> >
> >> > At each time i evaluate s it's different, do you have any idea ?
> >> >
> >> > Thank you
> >>
> >> You need to use the stream you're creating:
> >>
> >>
> >
> > stream = RandStream('mt19937ar','Seed',200); % Seed :200
> > s=rand(stream,5,1); % <- pass stream as an argument to rand
> >
> > Actually this is what I'm doing (that was a typo I'm sorry) and I don't
> > get the same results.
>
> Are you doing something like:
>
> stream = RandStream('mt19937ar','Seed',200); % Seed :200
> s1=rand(stream,5,1); % <- pass stream as an argument to rand
> s2=rand(stream,5,1); % <- pass stream as an argument to rand
>
> and finding that s1 and s2 are different? That's expected -- the first call
> to RAND (to generate s1) uses some of the numbers from the stream, so when
> you make the second call to RAND (to generate s2) you're at a different
> point in the stream. In essence, all the random number functions cause you
> to go downstream ;)
>
> If you RESET stream before the second call to RAND, are s1 and s2 equal?
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

It's working now thank you for your help!

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