Got Questions? Get Answers.
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:
random generator seed

Subject: random generator seed

From: Elnaz

Date: 25 Jan, 2013 20:48:07

Message: 1 of 5

Hi all,

I am running several MATLAB in parallel and I want to have my script running on several machines generate the same random numbers. To do this, I am doing
rng('default');
Is this the same as doing:
rand('state',0); % set state to 0

In other worsd, is the default setting '0'?

Thanks,
Elnaz

Subject: random generator seed

From: Bruno Luong

Date: 25 Jan, 2013 21:21:08

Message: 2 of 5


>> rand('state',0);
>> rand

ans =

    0.9501

>> rand('state',0);
>> rand

ans =

    0.9501

>> rng('default')
>> rand

ans =

    0.8147


And the answer is?

Bruno

Subject: random generator seed

From: Elnaz

Date: 25 Jan, 2013 22:11:08

Message: 3 of 5

Ok, so rng('default') is not equivalent of setting the seed to zero; but the real question is does rng('default') serve the purpose?
If I use rnd('state',0), I'll get the message that this syntax is no longer recommended; use rng function instead. So, the question is that if I put rng('default') in my script, then in all of my MATLAB applications regardless of their version (not older than 2010 but may be of different versions and are running on different machines) can I be certain that all random numbers generated will be the same?

Elnaz

Subject: random generator seed

From: Steven_Lord

Date: 25 Jan, 2013 22:27:47

Message: 4 of 5



"Elnaz " <ebsadeghian@gmail.com> wrote in message
news:kdur27$4sr$1@newscl01ah.mathworks.com...
> Hi all,
> I am running several MATLAB in parallel and I want to have my script
> running on several machines generate the same random numbers. To do this,
> I am doing rng('default'); Is this the same as doing:
> rand('state',0); % set state to 0

No, and you probably shouldn't be doing the latter if you can avoid it.

> In other worsd, is the default setting '0'?

No. See the page linked in the Note at the top of the Description section
for a more thorough explanation of what rng('default') is doing and why we
discourage the rand(string, seedValue) and randn(string, seedValue)
syntaxes.

http://www.mathworks.com/help/matlab/ref/rng.html

That page also lists the RNG equivalent for "rand('state', 0)" if you need
to reproduce results from older releases of MATLAB.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: random generator seed

From: Elnaz

Date: 25 Jan, 2013 22:41:07

Message: 5 of 5

Thanks Mr. Lord; I got my answer. So, I'll use rng(0) everywhere instead of rand('state',0) or randn('state',0).

Elnaz

Tags for this Thread

No tags are associated with 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