How to set a range for a random 4x3 Matrix

54 Ansichten (letzte 30 Tage)
Collin Jaegers
Collin Jaegers am 3 Sep. 2019
Bearbeitet: Adam Danz am 5 Okt. 2019
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.

Antworten (3)

Adam Danz
Adam Danz am 3 Sep. 2019
Bearbeitet: Adam Danz am 5 Okt. 2019
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
This is the recommended method by MathWorks in the documentation. They note,
191005 082428-Random Numbers Within a Specific Range - MATLAB & Simulink.png
  2 Kommentare
Bruno Luong
Bruno Luong am 4 Sep. 2019
Bearbeitet: Bruno Luong am 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
Guillaume am 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.

Melden Sie sich an, um zu kommentieren.


Bruno Luong
Bruno Luong am 4 Sep. 2019
Bearbeitet: Bruno Luong am 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 Kommentare
Bruno Luong
Bruno Luong am 4 Sep. 2019
No, it's not the same thing don't mix apple and orange.

Melden Sie sich an, um zu kommentieren.


Bruno Luong
Bruno Luong am 4 Sep. 2019
Bearbeitet: Bruno Luong am 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 Kommentare
Bruno Luong
Bruno Luong am 4 Sep. 2019
Bearbeitet: Bruno Luong am 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
Adam Danz am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Random Number Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by