From: Peter Perkins on
On 2/23/2010 6:46 AM, David Painter wrote:

> I want my randperms to produce different results on each run. How can I
> do it?

David, that depends on what you mean by "run".

1) If you mean, "each successive time I call it", then as John points out, it does that.

2) If you mean, "every time I start up MATLAB", then the right thing to do depends on what you are really doing.

a) It may be that you are running a Monte-Carlo simulation that depends on RANDPERM (and possibly other random number calls), and you want to run that simulation in several MATLAB sessions and think of the different runs as statistically independent so that you can combine the results, as if you had run one long simulation. Using a seed based on clock (for example) is one way to do that. However, you won't be able to go back and reproduce a run unless you save the seed. That may or may not be important to you, but another option would be to use one of the inherently parallel generator algorithms (e.g., 'mrg32k3a') and use a known stream index in each run.

b) It may be that you are running a MC simulation repeatedly, but not trying to combine the different runs as statistically independent, but are just uncomfortable having the same set of random values used each time. For example, you might be doing MC integration on different functions (you probably aren't doing that, but just as an illustration). You are free to feel uncomfortable, and you can do the same thing as in (a), but I might point out that if your conclusions from a MC simulation depends on the specific sequence of random values actually used, then you probably need to run more MC iterations.

c) You may just want to see a different output from RANDPERM each time you call it, regardless of whether or not you just started up MATLAB. That's fine (though I've never felt that need), go ahead and use clock. But there's no reason I know of to do it more than once per session, and you may just want to put something in your startup file.

Hope this helps.
From: Peter Perkins on
On 2/23/2010 6:19 PM, David Painter wrote:

> One call to seed the stream
> based on the clock is sufficient because clock is likely to be different
> on each call to:
>
> s = RandStream.create('mt19937ar','seed',sum(100*clock))
>
> Is that right?

It is, although

s = RandStream('mt19937ar','seed',sum(100*clock))

s marginally shorter to type (since you're creating only one random stream), and don't forget that you need to make your new stream the default random number stream,

RandStream.setDefaultStream(s)

You may find that simply reseeding the existing default stream

reset(RandStream.getDefaultStream,sum(100*clock))

at the beginning of each MATLAB session (e.g., in your startup file) is even easier.