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 numbers in parallel

Subject: random numbers in parallel

From: Gideon

Date: 26 Apr, 2013 00:01:58

Message: 1 of 4

I was wondering if people knew how robust the standard matlab rng function was when used in parallel matlab. The problem I have in mind just has a parfor loop and I'm generating monte carlo samples. I know there are issues when using random numbers in parallel to ensure each thread is generating independent samples. How is this handled in MATLAB? If I generically set rng(SEED) at the beginning of my code, then go into the parallel section, calling randn, will that be sufficient, or do I need to do something more sophisticated?

Subject: random numbers in parallel

From: Peter Perkins

Date: 26 Apr, 2013 04:01:03

Message: 2 of 4

On 4/25/2013 8:01 PM, Gideon wrote:
> I was wondering if people knew how robust the standard matlab rng function was when used in parallel matlab. The problem I have in mind just has a parfor loop and I'm generating monte carlo samples. I know there are issues when using random numbers in parallel to ensure each thread is generating independent samples. How is this handled in MATLAB? If I generically set rng(SEED) at the beginning of my code, then go into the parallel section, calling randn, will that be sufficient, or do I need to do something more sophisticated?

Gideon, on parallel workers, the default generator is mrg32k3a, which is
specifically designed for parallel simulation. Without knowing
specifically what you are doing, it's hard to say exactly what
initialization you might need to do, but it may be that you don't need
to do anything at all -- the workers are automatically set up with
parallel independent streams, and in many cases that's all you need.

Hope this helps.

Subject: random numbers in parallel

From: Gideon

Date: 26 Apr, 2013 13:39:35

Message: 3 of 4

Thanks Patrick. It sounds like I am in the "easy" case. The code I have is of the following form:

data = zeros(N,1);

parfor k = 1:N

xi = randn(M,1);
data(k) = f(xi);

end

In other words, I want N realizations of a certain function f which depends on having M i.i.d. Gaussian random variables. So all I want here is to ensure that each worker is getting an independent stream of random numbers when it goes on to use randn.

On Thursday, April 25, 2013 11:01:03 PM UTC-5, Peter Perkins wrote:
> On 4/25/2013 8:01 PM, Gideon wrote:
>
> > I was wondering if people knew how robust the standard matlab rng function was when used in parallel matlab. The problem I have in mind just has a parfor loop and I'm generating monte carlo samples. I know there are issues when using random numbers in parallel to ensure each thread is generating independent samples. How is this handled in MATLAB? If I generically set rng(SEED) at the beginning of my code, then go into the parallel section, calling randn, will that be sufficient, or do I need to do something more sophisticated?
>
>
>
> Gideon, on parallel workers, the default generator is mrg32k3a, which is
>
> specifically designed for parallel simulation. Without knowing
>
> specifically what you are doing, it's hard to say exactly what
>
> initialization you might need to do, but it may be that you don't need
>
> to do anything at all -- the workers are automatically set up with
>
> parallel independent streams, and in many cases that's all you need.
>
>
>
> Hope this helps.

Subject: random numbers in parallel

From: Steven_Lord

Date: 26 Apr, 2013 17:12:35

Message: 4 of 4



"Gideon" <gideon.simpson@gmail.com> wrote in message
news:28fe2c6f-c9e0-49fb-9514-f59287e53b1b@googlegroups.com...
> Thanks Patrick.

Peter is the one to whom you responded.

> It sounds like I am in the "easy" case. The code I have is of the
> following form:
>
> data = zeros(N,1);
>
> parfor k = 1:N
>
> xi = randn(M,1);
> data(k) = f(xi);
>
> end
>
> In other words, I want N realizations of a certain function f which
> depends on having M i.i.d. Gaussian random variables. So all I want here
> is to ensure that each worker is getting an independent stream of random
> numbers when it goes on to use randn.

You might find this section of the documentation interesting and/or
reassuring.

http://www.mathworks.com/help/distcomp/control-random-number-streams.html

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

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