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 number generation and initial seed

Subject: Random number generation and initial seed

From: lmetrinko@my-deja.com

Date: 11 Jan, 2001 14:48:44

Message: 1 of 4

I have several simulations, originally written in
C, that require random number generation. I
translated the programs to MATLAB, but apparently
the random number generator initializes with the
same seed every time. I have inserted the
following line at the beginning of the code:
s=rand('state',sum(100*clock));
because the references I have seem to indicate
that the random number generator will be set to a
different state if you initialize with the clock.
Can anyone confirm that this is correct?
If not, suggestions would be greatly appreciated.
Thanks, Laura M


Sent via Deja.com
http://www.deja.com/

Subject: Random number generation and initial seed

From: Denis Gilbert

Date: 11 Jan, 2001 15:06:55

Message: 2 of 4

lmetrinko@my-deja.com wrote in article <93kh4a$3sb$1@nnrp1.deja.com>...
> I have inserted the
> following line at the beginning of the code:
> s=rand('state',sum(100*clock));
> because the references I have seem to indicate
> that the random number generator will be set to a
> different state if you initialize with the clock.
> Can anyone confirm that this is correct?


Yes, this is correct.

Subject: Random number generation and initial seed

From: Mike Vaughn

Date: 14 Jan, 2001 01:59:26

Message: 3 of 4

In article <93kh4a$3sb$1@nnrp1.deja.com>, lmetrinko@my-deja.com wrote:

> I have several simulations, originally written in
> C, that require random number generation. I
> translated the programs to MATLAB, but apparently
> the random number generator initializes with the
> same seed every time. I have inserted the
> following line at the beginning of the code:
> s=rand('state',sum(100*clock));
> because the references I have seem to indicate
> that the random number generator will be set to a
> different state if you initialize with the clock.
> Can anyone confirm that this is correct?
> If not, suggestions would be greatly appreciated.
> Thanks, Laura M


This is a popular recommendation, and a very reasonable one,
unless you are following one calculation closely after another.

An alternative is to save the seed at the end of one calculation,
and use it to seed the next one. This should work, but in
the MatLAB 5.2 and later generators, I do not see how to
restore the full state of the RNG (for a hint of the
technical details, see the onlone help file). But at least
you will not be repeating the previous calculation.

Subject: Random number generation and initial seed

From: Nabeel

Date: 13 Jan, 2001 21:51:11

Message: 4 of 4

Hi,

> An alternative is to save the seed at the end of one calculation,
> and use it to seed the next one. This should work, but in
> the MatLAB 5.2 and later generators, I do not see how to
> restore the full state of the RNG (for a hint of the
> technical details, see the onlone help file).

I believe the help shows how to set (or restore) and obtain the random
number generator's seed. For example:

>> rand('state',sum(100*clock)); % Set a random state based on clock
>> State = rand('state'); % get the state
>> numbers = rand(1,4)
numbers =
    0.5215 0.5792 0.1521 0.1662
>> rand('state',State); % restore the original state
>> same_numbers_as_before = rand(1,4)
same_numbers_as_before =
    0.5215 0.5792 0.1521 0.1662
>> all(numbers == same_numbers_as_before)
ans =
     1

hope ths clarifies things,

Nabeel

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