# Equally distributed multidimensional random values with boundaries - how to generate?

18 views (last 30 days)

Show older comments

I have to generate a matrix that will have 100 columns. Every row represents a value that can change in defined range. For example, I can describe it by the array:

A=[1 5; 3 7; 1 10]

Where 1 to 5 is first row range, 3 to 7 is the second, and 1 to 10 is the last. If I want to generate the random distribution to cover the range, just for one line, I can do this as follow:

data = lb + rand(1,100) .* ( ub - lb );

Where ub and lb are upper and lower boundary. Now, I can reproduce this in simple for loop:

for i=1:size(A,1)

lb=A(i,1);

ub=A(i,2);

data2(i,:) = lb + rand(1,100) .* ( ub - lb );

end

But in this case, every single row is evaluated separately, So I don't have any guarantee that the distribution will be equal in the meaning of comibinations between rows, as every rows changes independent. For example I can encounter situation where I will not have any combination with Row 1 close to 1 and Row 2 close to 7, just because of RNG. Is there any way I can sovle my problem and ensure multidimensional equal random distribution?

##### 7 Comments

John D'Errico
on 13 Feb 2023

Edited: John D'Errico
on 13 Feb 2023

I'm sorry, but I think you still misunderstand random numbers, what a uniform distribution means, and, apparently the entire point of my comment.

That you have columns with different ranges is completely irrelevant. Each column will be filled with sets of numbers that are uniformly distributed. And they are independent of other columns, or of previous samples.

For example:

n = 25000;

X = [rand(n,1),rand(n,1)*2 + 1];

So the first column of X (thus X(:,1)) is uniformly distributed, on the open interval (0,1).

X(:,2) is niformly distributed on the open interval (1,3).

These points, if taken as points in the two dimensional box (0,1)x(1,3), will fill that space uniformly. Of course if the sampling is coarse enough, the box will be filled in very well.

plot(X(:,1),X(:,2),'.')

If I choose n a bit larger, then the figure turns completely blue, with white showing through at all. And if you count the number of points in any local region of the box, so essentially a 2-dimensional histogram, then you would find that locally the number of points in that region will be proportional to the area of the region you looked at.

For example, histcounts produces that 2-d histogram.

[N,XEDGES,YEDGES] = histcounts2(X(:,1),X(:,2))

And we would expect to see on average, with a 10x10 grid of bins on that domain, we would expect to see 1% of the samples falling in each bin. Indeed, that is what happens. If the sample size were larger, then the counts in each bin will more accurately approach that value of 1% in each bin. We expect to see some degree of variability of course in those bin counts, but as I have said, that will decrease with sample size.

surf(N)

That the different sets of variables live in different intervals is completely irrelevant. (Sorry, I forgot to scale the x and y axes in the 2-d hstogram plot.)

### Accepted Answer

William Rose
on 13 Feb 2023

##### 8 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!