# How to efficiently create a matrix of random numbers generating independently?

Asked by Hsinho on 1 Jul 2012
Latest activity Commented on by Hsinho on 2 Jul 2012

Hi,

I want to have a 100-by-2000 matrix of random numbers. Each column of random numbers is generated by an independent stream. It means there should be 2000 independent streams.

I learn from "Help: Create three independent streams"

```[s1,s2,s3] = RandStream.create('mrg32k3a','NumStreams',3);
r1 = rand(s1,100000,1); r2 = rand(s2,100000,1); r3 = rand(s3,100000,1);
```

It is very easy with 3 independent streams.

How can I efficiently create this matrix of random numbers from 2000 independent streams?

Thank you!

Jan Simon on 1 Jul 2012

Are you sure that you need 2000 independent streams? Will you be able to distinguish it from rand(100, 20000)?

Richard Brown on 2 Jul 2012

+1 Jan

Hsinho on 2 Jul 2012

My first attempt to generate the 100-by-2000 matrix with random numbers is simply using "rand" or "randi."

The three main steps involving random numbers generation in the script are as followed.

```len=100;
```
```R=2000;
```
```y=zeros(len,R);
```
```for n=1:2.1e7
```
```N=(0:len:(R-1)*len)+randi(len,1,R); % Only one element in each column was randomly selected.
```
```y(N)=-1+9*rand(1,R); % The selected element of y matrix was replaced with a random number uniformly distributed from -1 to 8.
```
```A=find(deltaE>0); %Compare E values calculated from y values in each column
```
```exp(-delta_E/k/T)>rand(1,length(A)); %Accept E values when it meets the criterion.
```
```end
```

When this for-loop finished, I observed clearly that the y values in the last 200 to 500 columns fluctuated around zero or still are zero but the all other y values from column 1 to column 1500 vary between -1 and 8.

I don't understand why this happen. I expected all columns should vary between -1 and 8.

The goal I want to reach is that all the random number generating(random N, random y values, rand(1,length(A)) in each column is independent to each other. The calculation of E value in each column is a independent simulation (i.e. I want to do 2000 independent simulations simultaneously).

However,the present results I have is that some columns seems untouched.

How can I modify the script to accomplish my goal?

Thanks !

## Products

No products are associated with this question.