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:
Simple RNG?!

Subject: Simple RNG?!

From: Peter Mairhofer

Date: 6 Jun, 2011 18:46:24

Message: 1 of 6

Hi,

Last night I did excessive simulations, burned dozens of kWhs ... just
to see now that it was all for nothing: And I have to repeat. Everytime
the same results. And why? Obviously MATLAB is unable to correctly
initialize its PRNG (a fact I did not realize until now).

Is there a way to

a.) *always* seed the PRNG at startup with a *random* value?

b.) An easy way to seed the PRNG?

All the docs are only talking about "static" seeds - but where to get a
real one?

The documentation is even keen to mention "sum(100*clock)" as a seed.
But this must be a joke. On a simulation cluster, starting hundreds of
my simulations simultanously this would easy lead to the same seed.

Currently my "solution" is a startup script:

#!/bin/bash
matlab -glnx86 -nosplash -nojvm -nodesktop -nodisplay -r "clear
all;seed=$RANDOM;run ./script.m"

with script.m:

reset(RandStream.getDefaultStream(), seed);
...


But this can't be the solution? Is there a way to ensure random for all
simulations without any hacks?

Thank you, regards,
Peter

Subject: Simple RNG?!

From: Jan Simon

Date: 6 Jun, 2011 19:56:02

Message: 2 of 6

Dear Peter,

You can use random numbers from an external source for seeding, e.g.:
http://www.mathworks.com/matlabcentral/fileexchange/27942-www-random-org-random-integerssequencesstrings
http://www.mathworks.com/matlabcentral/fileexchange/27849-randiorg-true-random-integer-numbers
http://www.mathworks.com/matlabcentral/fileexchange/21353-true-random-integer-generator

Another idea is a creating a server function for the seeds: Each client asks for a seed and the server replies random numbers from a single stream.

Kind regards, Jan

Subject: Simple RNG?!

From: Jeff

Date: 6 Jun, 2011 20:10:23

Message: 3 of 6

Peter Mairhofer <63832452@gmx.net> wrote in message <isj7b6$19c$1@news.albasani.net>...
> Hi,
>
> Last night I did excessive simulations, burned dozens of kWhs ... just
> to see now that it was all for nothing: And I have to repeat. Everytime
> the same results. And why? Obviously MATLAB is unable to correctly
> initialize its PRNG (a fact I did not realize until now).
>
> Is there a way to
>
> a.) *always* seed the PRNG at startup with a *random* value?
>
> b.) An easy way to seed the PRNG?
>
> All the docs are only talking about "static" seeds - but where to get a
> real one?
>
> The documentation is even keen to mention "sum(100*clock)" as a seed.
> But this must be a joke. On a simulation cluster, starting hundreds of
> my simulations simultanously this would easy lead to the same seed.
>
> Currently my "solution" is a startup script:
>
> #!/bin/bash
> matlab -glnx86 -nosplash -nojvm -nodesktop -nodisplay -r "clear
> all;seed=$RANDOM;run ./script.m"
>
> with script.m:
>
> reset(RandStream.getDefaultStream(), seed);
> ...
>
>
> But this can't be the solution? Is there a way to ensure random for all
> simulations without any hacks?
>
> Thank you, regards,
> Peter
>

The only built in random string generator in Matlab I know of is DICOMUID, which among other things appears to serve as a gateway to the various UID commands on various platforms. e.g. uuidgen for Unix, Mac etc. This should help you make unique seeds, at least.

Subject: Simple RNG?!

From: Steven_Lord

Date: 7 Jun, 2011 13:20:43

Message: 4 of 6



"Peter Mairhofer" <63832452@gmx.net> wrote in message
news:isj7b6$19c$1@news.albasani.net...
> Hi,
>
> Last night I did excessive simulations, burned dozens of kWhs ... just to
> see now that it was all for nothing: And I have to repeat. Everytime the
> same results. And why? Obviously MATLAB is unable to correctly initialize
> its PRNG (a fact I did not realize until now).

It does initialize the PRNG correctly, but perhaps not in the way you wanted
it to be initialized.

When you start a new MATLAB session, the PRNG is ALWAYS set to start with
the same exact seed value. This means that if you start MATLAB and run a
simulation, you can start another MATLAB session and run that same
simulation to obtain the same exact results (assuming that you're not using
the clock, a file, or some other method that different between the two
sessions to generate a seed.) Reproducibility can be a Good Thing -- after
all, if you run a simulation and find some "interesting" results at the end
it would be useful to be able to run that simulation and watch the progress
to see how those interesting results occurred.

> Is there a way to
>
> a.) *always* seed the PRNG at startup with a *random* value?

What is your definition for a "random value"? What would YOU consider to be
a truly random value?

If you mention /dev/random, then you should read reference 5 on this
Wikipedia page:

http://en.wikipedia.org/wiki/Urandom

> b.) An easy way to seed the PRNG?

Which version of MATLAB are you using? If you're using release R2011a or
later, use RNG.

http://www.mathworks.com/help/techdoc/rn/bsq8kw5-1.html#bsvbbbz-1

If you're using release R2008b or later, you can use a RandStream object.

http://www.mathworks.com/help/techdoc/rn/brqyzsl-1.html#brqyzsl-3

For earlier releases, you will need to use the RAND or RANDN functions with
the 'seed', 'state', or 'twister' keywords. But if you're using R2008b or
later, please don't use this approach -- RNG or RandStream are preferred.

> All the docs are only talking about "static" seeds - but where to get a
> real one?

What do you mean when you say "real one"?

> The documentation is even keen to mention "sum(100*clock)" as a seed. But
> this must be a joke. On a simulation cluster, starting hundreds of my
> simulations simultanously this would easy lead to the same seed.

On a cluster no, that's not a very good choice of seed. On one individual
machine, if you need a number that "looks" random as a seed? It's not that
bad.

Since you mention that you're using a cluster, are you using Parallel
Computing Toolbox? If you are, and you're using release R2009b, don't set
the seed on the workers unless you absolutely need to do so.

http://www.mathworks.com/help/toolbox/distcomp/rn/br46db1-1.html#br_ni3m-1

> Currently my "solution" is a startup script:
>
> #!/bin/bash
> matlab -glnx86 -nosplash -nojvm -nodesktop -nodisplay -r "clear
> all;seed=$RANDOM;run ./script.m"
>
> with script.m:
>
> reset(RandStream.getDefaultStream(), seed);
> ...
>
>
> But this can't be the solution? Is there a way to ensure random for all
> simulations without any hacks?

You don't really want to do that, do you? As written, there's no real way to
reproduce the behavior of your MATLAB session, since you don't know what
seed was when script.m was executed. At the very least you should display
(in FORMAT HEX) the exact seed you're using into whatever simulation log
files you create.

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

Subject: Simple RNG?!

From: Peter Perkins

Date: 20 Jun, 2011 14:09:54

Message: 5 of 6

On 6/6/2011 2:46 PM, Peter Mairhofer wrote:
> Last night I did excessive simulations, burned dozens of kWhs ... just
> to see now that it was all for nothing: And I have to repeat. Everytime
> the same results. And why? Obviously MATLAB is unable to correctly
> initialize its PRNG (a fact I did not realize until now).
>
> Is there a way to
>
> a.) *always* seed the PRNG at startup with a *random* value?
>
> b.) An easy way to seed the PRNG?

Peter, you probably should be using the parallel random number generator
features in MATLAB, substreams being the most straight-forward. The
MATLAB User Guide sections on random number generation describe all of
that, but you will most likely find these two blog posts a more
approachable starting point:

<http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/>
<http://blogs.mathworks.com/loren/2008/11/13/new-ways-with-random-numbers-part-ii/>

If you are using R2011a, the new RNG function provides a very simple
front-end to the most common kinds of random number generator "control",
but if you are running many replications of the same simulation, you
probably want to use RandStream, and that's what those blog posts will
get you up and running on.

Hope this helps.

Subject: Simple RNG?!

From: Greg Heath

Date: 21 Jun, 2011 04:15:24

Message: 6 of 6

On Jun 6, 2:46 pm, Peter Mairhofer <63832...@gmx.net> wrote:
> Hi,
>
> Last night I did excessive simulations, burned dozens of kWhs ... just
> to see now that it was all for nothing: And I have to repeat. Everytime
> the same results. And why? Obviously MATLAB is unable to correctly
> initialize its PRNG (a fact I did not realize until now).
>
> Is there a way to
>
> a.) *always* seed the PRNG at startup with a *random* value?
>
> b.) An easy way to seed the PRNG?
>
> All the docs are only talking about "static" seeds - but where to get a
> real one?
>
> The documentation is even keen to mention "sum(100*clock)" as a seed.
> But this must be a joke. On a simulation cluster, starting hundreds of
> my simulations simultanously this would easy lead >to the same seed.

The computer calulates much faster than
the clock updates. With my old computer
the clock updates approximately 60 times
per second

Therefore the use of the command

pause(1/60)

can yield

60*60 = 3600 initializations per minute

My old RNG has

2^31 = 2,147,483,648

integer initialization states. However,
100*sum(clock) is definitely not recommended
for generating them.

60 times a second, clock will output time to
1000th of a second. Therefore, in 2011,
1000*sum(clock) will only generate 183,984
integers within the interval bounds
Note: 60-1/60 = 59.9833...)

1000*(2011+1+1) = 2,013,000
1000*(2011+12+31+24+59+59.983) = 2,196,983

This interval or even a smaller one can be
readily scaled to the range [1,2^13].
However, there will be no more than 183,984
states selected.

Nevertheless, if you have access to MATLAB's
newer RNGs, I suggest you learn how to use
them.

Hope this helps

Greg

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