MATLAB: Size input must be scalar using rand to generate uniformly distributed numbers

random number generatoruniform distribution

I want to generate a set of random numbers which contains 1000 uniformly distributed numbers on the interval [-1,1]. I am getting the error that input must be scalar.
my code:
a = rand(1000,[-1,1]);
Error using rand
Size inputs must be scalar.

Best Answer

  • rand() does not accept the range of random values. It always generates values between 0 and 1. You need to rescale yourself.
    lb = -1;
    ub = 1;
    a = rand(1000,1)*(ub-lb) + lb; % 1000x1 vector