Curious as to why randi can only take on values that are positive, scalar integers.

조회 수: 2 (최근 30일)
Ebony
Ebony 2016년 1월 25일
편집: Stephen23 2016년 1월 25일
I'm new, terrible at coding, and know little to nothing about this software, so this question is going to sound rather foolish, but I'm just curious as to why these sorts of restrictions exist for this function, I'll likely ask this question again for other aspects too.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 25일
Because that is what the function is for, to produce random integers.
You are incorrect that it only accepts positive scalars: the first argument can be a two element vector of minimum and maximum integers for the range, such as
randi([-50,100],5)
to produce a 5 x 5 in the range -50 to +100
For continuous values in a uniform distribution, use
rand(SIZE_OF_ARRAY) * (MaximumValue - MinimumValue) + MinimumValue
For discrete values that are non-integer or not contiguous, see randsample()

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by