How to set a range for a random 4x3 Matrix
Show older comments
I am trying to write an expression to create a 4x3 matrix of floating random numbers, each in the inclusive range from -10 to 10.
Answers (3)
bounds = [-10,10];
x = rand(4,3) * range(bounds) + bounds(1); % for decimals
x = randi(bounds,4,3); % for integers
Test it:
bounds = [-10,10];
x = rand(4000,3000) * range(bounds) + bounds(1);
[min(x(:)),max(x(:))]
% ans =
% -10 10

2 Comments
Bruno Luong
on 4 Sep 2019
Edited: Bruno Luong
on 4 Sep 2019
Rand() never returns 0 or 1
x = rand(4000,3000) * 20 + bounds(1);
min(x(:))+10
max(x(:))-10
So nope (you get caught by MATLAB formating)
>> min(x(:))+10
ans =
4.7838e-07
>> max(x(:))-10
ans =
-1.8395e-06
>>
Guillaume
on 4 Sep 2019
Rand() never returns 0 or 1
Yes, for some reason doc rand doesn't mention anymore that the interval is open. help rand still does. I don't understand why that has been removed.
On the other hand, it hardly matters if the interval is open or close, the probability of getting any particularly number, be it 0, 1 (if the interval was close), 1/2, pi, is so small as to be negligible.
Bruno Luong
on 4 Sep 2019
Edited: Bruno Luong
on 4 Sep 2019
Inclusive and float
x = max(min(((rand(4,3)-0.5)*20)*1.001,10),-10)
you get 1/1000 chance to hit -10/+10
3 Comments
you get 1/1000 chance to hit -10/+10
So the distribution is heavily skewed towards these two values and no longer uniform.
>> x = max(min(((rand(1e6,1)-0.5)*20)*1.001,10),-10);
>> histogram(x, -10:0.01:10)

Adam Danz
on 4 Sep 2019
Bruno Luong
on 4 Sep 2019
No, it's not the same thing don't mix apple and orange.
Bruno Luong
on 4 Sep 2019
Edited: Bruno Luong
on 4 Sep 2019
In principle it can reach -10/+10 with tiny strict positive probability, but won't cover interval with a fine density
x = ((randi(2^53-1,4,3)-1)/(2^53-2)-0.5)*20-10
Tiny probability with a fine density:
x = max(min(((rand(4,3)-0.5)*20)*(1+eps()),10),-10)
not sure if the second method makes any difference with what proposed Adam
x = rand(4,3) * 20-10
3 Comments
Guillaume
on 4 Sep 2019
I think you've got a rogue offset in your first option.
My understanding is that the density of that option is more or less the same as rand (with the default mersenne twister generator) where all numbers are multiples of
. See there.
So, of course, the probability of seeing -10 or 10 is 1/2^53 ~= 1e-16. I'm not sure it's worth expanding such efforts to go from it can't generate -10 or 10 (adam's method) to it can theoretically generate -10 or 10 but you'll never see it (your method). Even if you generate 1 million numbers per second, it would still take you around 285 years before you see a single exact 10.
Bruno Luong
on 4 Sep 2019
Edited: Bruno Luong
on 4 Sep 2019
Well I agree, but I'm not the one who asks for "inclusive", you should comment on OP's question.
Adam Danz
on 6 Sep 2019
I'd bet that the need for a random process would have higher importance than the need for the very small chance of hitting the bounds. In cases where both are important a better solution would be to expand the bounds by a very very small amount near the limit of Matlab's precision and then to include a conditional that checks if a value was drawn outside of the bounds. In that case there can be another random draw. That way the bounds are included and have the same uniform probability of being chosen as any other random value.
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!