Thread Subject:
MATLAB produces the same random number after being restarted

Subject: MATLAB produces the same random number after being restarted

From: Tung Le

Date: 19 Jun, 2012 21:23:09

Message: 1 of 7

Hello,

I have a question on random number generator in MATLAB.
If I generate 10 random numbers like randi(10,[1 10]), and immediately after that I restart MATLAB and do the same thing, I will get exactly the same set of numbers.
So I want to ask if there is a way to break this issue.

This issue rises when I run a simulation with MATLAB in the command mode.
I want to run the same program for several times to get the statistics but it keeps producing exactly the same data everytime.

Is there a command that will break the computer clock or something that can make MATLAB to start with a new random number?

Thanks,
Tung

Subject: MATLAB produces the same random number after being restarted

From: Matt J

Date: 19 Jun, 2012 21:49:08

Message: 2 of 7

"Tung Le" <dexturelab@yahoo.com> wrote in message <jrqqjt$bu0$1@newscl01ah.mathworks.com>...
>
> Is there a command that will break the computer clock or something that can make MATLAB to start with a new random number?
===============

Yes. The RNG command.

Subject: MATLAB produces the same random number after being restarted

From: Tung Le

Date: 19 Jun, 2012 22:10:08

Message: 3 of 7

"Matt J" wrote in message <jrqs4k$i62$1@newscl01ah.mathworks.com>...
> "Tung Le" <dexturelab@yahoo.com> wrote in message <jrqqjt$bu0$1@newscl01ah.mathworks.com>...
> >
> > Is there a command that will break the computer clock or something that can make MATLAB to start with a new random number?
> ===============
>
> Yes. The RNG command.

Could you make it a little bit more clear?
When I typed RNG into cmd, it couldn't recognize this command.

For example,
In cmd, I typed:
    matlab -r "randi(10, [1 10])"
then
    rng ===> not working!

Thank you.
Tung

Subject: MATLAB produces the same random number after being restarted

From: Matt J

Date: 19 Jun, 2012 22:49:08

Message: 4 of 7

"Tung Le" <dexturelab@yahoo.com> wrote in message <jrqtc0$n1a$1@newscl01ah.mathworks.com>...
>
> Could you make it a little bit more clear?
> When I typed RNG into cmd, it couldn't recognize this command.
>
> For example,
> In cmd, I typed:
> matlab -r "randi(10, [1 10])"
> then
> rng ===> not working!
====================

RNG is a MATLAB command, not a cmd command. Run "doc rng" within a MATLAB session to learn its usage. I think you probably want the syntax

rng('shuffle');

I don't recommend trying to control the conditions in which MATLAB starts up from outside MATLAB. You should use a startup.m file (see "docsearch startup").

I would also think carefully whether you want the random number generator to run differently every time MATLAB starts up. It will make your work hard to reproduce. You can always issue RNG commands inside particular code routines in the particular cases when you're certain you want non-reproducible behavior.

 

Subject: MATLAB produces the same random number after being restarted

From: Tung Le

Date: 20 Jun, 2012 02:09:08

Message: 5 of 7

"Matt J" wrote in message <jrqvl4$2kj$1@newscl01ah.mathworks.com>...
> "Tung Le" <dexturelab@yahoo.com> wrote in message <jrqtc0$n1a$1@newscl01ah.mathworks.com>...
> >
> > Could you make it a little bit more clear?
> > When I typed RNG into cmd, it couldn't recognize this command.
> >
> > For example,
> > In cmd, I typed:
> > matlab -r "randi(10, [1 10])"
> > then
> > rng ===> not working!
> ====================
>
> RNG is a MATLAB command, not a cmd command. Run "doc rng" within a MATLAB session to learn its usage. I think you probably want the syntax
>
> rng('shuffle');
>
> I don't recommend trying to control the conditions in which MATLAB starts up from outside MATLAB. You should use a startup.m file (see "docsearch startup").
>
> I would also think carefully whether you want the random number generator to run differently every time MATLAB starts up. It will make your work hard to reproduce. You can always issue RNG commands inside particular code routines in the particular cases when you're certain you want non-reproducible behavior.
>
>

It's working now.
Thank you.

Subject: MATLAB produces the same random number after being restarted

From: Greg Heath

Date: 20 Jun, 2012 02:50:08

Message: 6 of 7

"Tung Le" <dexturelab@yahoo.com> wrote in message <jrqtc0$n1a$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <jrqs4k$i62$1@newscl01ah.mathworks.com>...
> > "Tung Le" <dexturelab@yahoo.com> wrote in message <jrqqjt$bu0$1@newscl01ah.mathworks.com>...
> > >
> > > Is there a command that will break the computer clock or something that can make MATLAB to start with a new random number?
> > ===============
> >
> > Yes. The RNG command.
>
> Could you make it a little bit more clear?
> When I typed RNG into cmd, it couldn't recognize this command.

Whenever you want information about a name or command, use the commands
help, doc and type. For example,

help rng
doc rng
type rng

Hope this helps.

Greg

Subject: MATLAB produces the same random number after being restarted

From: Steven_Lord

Date: 20 Jun, 2012 14:00:06

Message: 7 of 7



"Tung Le" <dexturelab@yahoo.com> wrote in message
news:jrqqjt$bu0$1@newscl01ah.mathworks.com...
> Hello,
>
> I have a question on random number generator in MATLAB.
> If I generate 10 random numbers like randi(10,[1 10]), and immediately
> after that I restart MATLAB and do the same thing, I will get exactly the
> same set of numbers.

That's correct.

http://www.mathworks.com/help/techdoc/math/bs1qb_i.html

Read through the "Starting Over" portion of that page.

> So I want to ask if there is a way to break this issue.
>
> This issue rises when I run a simulation with MATLAB in the command mode.
> I want to run the same program for several times to get the statistics but
> it keeps producing exactly the same data everytime.
>
> Is there a command that will break the computer clock or something that
> can make MATLAB to start with a new random number?

As others have said, have your function call RNG (once; don't call it
repeatedly in a loop hoping to make the numbers "more random" or anything
like that -- that tends to make the numbers "less random" in some sense, if
I recall correctly.)

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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
rng Tung Le 19 Jun, 2012 18:14:09
randi Tung Le 19 Jun, 2012 17:24:09
rssFeed for this Thread

Contact us