Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Resetting the state of Rand randomly at start up

Subject: Resetting the state of Rand randomly at start up

From: Sourav Roy

Date: 21 May, 2008 04:26:03

Message: 1 of 7

Hi,
I was using rand to simulate a price series, but rand
function returns the same rand no every time on start up,
hence I am not getting truly random price paths. To overcome
this I was thinking of changing the state of the rand series
generator to some random or non repeating variable, one such
variable could be system date and time but don't know how to
do this.
Can some one help with this.

For reference the manual on rand function reads :-

Quote

The sequence of numbers produced by rand is determined
by the internal state of the generator. Setting the
generator to the same fixed state enables you to repeat
computations. Setting the generator to different states
leads to unique computations. It does not, however, improve
statisticalproperties.
Because MATLAB resets the rand state at startup, rand
generates the same sequence of numbers in each session
unless you change the value of the state input.

UnQuote



Subject: Resetting the state of Rand randomly at start up

From: Ken Campbell

Date: 21 May, 2008 04:44:04

Message: 2 of 7

"Sourav Roy" <souravroy1@rediffmail.com> wrote in message
<g1088r$lfr$1@fred.mathworks.com>...
> Hi,
> I was using rand to simulate a price series, but rand
> function returns the same rand no every time on start up,
> hence I am not getting truly random price paths. To
overcome
> this I was thinking of changing the state of the rand
series
> generator to some random or non repeating variable, one
such
> variable could be system date and time but don't know how
to
> do this.
> Can some one help with this.
>
> For reference the manual on rand function reads :-
>
> Quote
>
> The sequence of numbers produced by rand is determined
> by the internal state of the generator. Setting the
> generator to the same fixed state enables you to repeat
> computations. Setting the generator to different states
> leads to unique computations. It does not, however,
improve
> statisticalproperties.
> Because MATLAB resets the rand state at startup, rand
> generates the same sequence of numbers in each session
> unless you change the value of the state input.
>
> UnQuote
>
>
>

I think

rand('twister',sum(10*clock))

would randomize the seed (at least for the Mersenne twister
mode) for you.

Ken

Subject: Resetting the state of Rand randomly at start up

From: Kenneth Eaton

Date: 21 May, 2008 04:48:04

Message: 3 of 7

I think this is what most people typically do:

rand('state',sum(100.*clock));

Ken

Subject: Resetting the state of Rand randomly at start up

From: Sourav Roy

Date: 21 May, 2008 04:57:02

Message: 4 of 7

"Kenneth Eaton" <Kenneth.dot.Eaton@cchmc.dot.org> wrote in
message <g109i4$5t1$1@fred.mathworks.com>...
> I think this is what most people typically do:
>
> rand('state',sum(100.*clock));
>
> Ken

Hi,
Thanks I would try this.

Subject: Resetting the state of Rand randomly at start up

From: helper

Date: 21 May, 2008 05:52:02

Message: 5 of 7

"Sourav Roy" <souravroy1@rediffmail.com> wrote in message
<g1088r$lfr$1@fred.mathworks.com>...
> Hi,
> I was using rand to simulate a price series, but rand
> function returns the same rand no every time on start up,
> hence I am not getting truly random price paths. To
overcome
> this I was thinking of changing the state of the rand
series
> generator to some random or non repeating variable, one
such
> variable could be system date and time but don't know how
to
> do this.
> Can some one help with this.
>
> For reference the manual on rand function reads :-
>
> Quote
>
> The sequence of numbers produced by rand is determined
> by the internal state of the generator. Setting the
> generator to the same fixed state enables you to repeat
> computations. Setting the generator to different states
> leads to unique computations. It does not, however,
improve
> statisticalproperties.
> Because MATLAB resets the rand state at startup, rand
> generates the same sequence of numbers in each session
> unless you change the value of the state input.
>
> UnQuote
>
>
>

Here is the best method for avoiding this behavior. Create
a file called startup.m in the directory MATLAB starts up
in. Within startup.m enter the code:

%%%%%
lastState = load('randstate.mat','randnState','randState');
rand('state',lastState.randState)
randn('state',lastState.randnState)
%%%%%

Then, create a "finish.m" file in the same location with
the following code:

%%%%%
randnState = randn('state');
randState = rand('state');
save('randstate.mat','randnState','randState');
%%%%%%

What this does is automatically saves your random state
whenever you close MATLAB then automatically reloads that
state when your restart MATLAB. This will esentially "turn
off" MATLAB's behavior of resetting it's random state at
startup.

There is nothing significantly wrong with the "rand
('twister',sum(10*clock))" method, but the method I mention
is cleaner and more effectively ensures your random state
doesn't get "polluted".

Note: The startup.m file is automatically run by MATLAB on
startup. Also, the finish.m file is automatically run by
MATLAB on quitting. You can put any code in these files
you want to have automatically run.

Subject: Resetting the state of Rand randomly at start up

From: helper

Date: 21 May, 2008 06:04:05

Message: 6 of 7

Ooops. I've kept this code from old versions of MATLAB
(pre-twister). I guess what you really want to do is:

startup.m
%%%%%
lastState = load('randstate.mat','randnState','randState');
rand('twister',lastState.randState)
randn('state',lastState.randnState)
%%%%%

finish.m
%%%%%
randnState = randn('state');
randState = rand('twister');
save('randstate.mat','randnState','randState');
%%%%%%

Subject: Re: Resetting the state of Rand randomly at start up

From: Greg Heath

Date: 21 May, 2008 07:54:36

Message: 7 of 7

On May 21, 12:57=A0am, "Sourav Roy" <souravr...@rediffmail.com> wrote:
> "Kenneth Eaton" <Kenneth.dot.Ea...@cchmc.dot.org> wrote in
> message <g109i4$5t...@fred.mathworks.com>...
>
> > I think this is what most people typically do:
>
> > rand('state',sum(100.*clock));
>
> > Ken
>
> Hi,
> Thanks I would try this.

Using 1e5 instead of 100 is better.

http://groups.google.com/group/comp.soft-sys.matlab/msg/a235d956cda15e4a

Hope this helps.

Greg

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
random nos Sourav Roy 21 May, 2008 00:30:24
simulation Sourav Roy 21 May, 2008 00:30:24
matlab Sourav Roy 21 May, 2008 00:30:24
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics