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

4 views (last 30 days)
Ebony on 25 Jan 2016
Edited: Stephen Cobeldick on 25 Jan 2016
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 Comment

Stephen Cobeldick on 25 Jan 2016
The randi documentation explains how to get negative integers and non-scalar arrays:
>> randi([-4,4],3)
ans =
-2 -1 -1
0 -3 -2
4 4 1
The documentation tells us what MATLAB can do. Use it!

Walter Roberson on 25 Jan 2016
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()