In: Computer Science
The command below produces a row vector of 100 random values uniformly distributed between -10 and +10.
r = 10.*rand(1,100)-10;
r = 20.*rand(1,100)-10;
r = -10.*rand(1,100)+10;
r = 10.*rand(1,100)-20;
r = 20.*rand(1,100)-10; is the right answer
Explanation
A general formula to produce a row vector of X random values uniformly distributed between some values A and B could be given as:
r = (B-A).*rand(1,X)+A;
in this example consider A = -10 and B = 10
Let's break it down
(1,100) means that the matrix has 1 row and 100 columns, which is nothing but a row vector.
rand function returns real numbers that are between 0 and 1, in a uniform manner.
Why are we multiplying the random number thus generated with 20 and then subtracting it by 10?
By multiplying 20 with the random number thus generated (which is between 0 and 1), we get a random number which is between 0 and 20.
When we subtract this number (which is between 0 and 20) with 10, the result is a number which is between -10 and 10
Example 1:
the rand function generated 0.1
0.1 x 20 = 2
2 -10 = -2
-2 is between -10 and 10
Example 2:
the rand function generated 0.9
0.9 x 20 = 18
18 -10 = 8
8 is between -10 and 10
The first option r = 10.*rand(1,100)-10; seems to be correct for instance.
But it is wrong because it only generates numbers between -10 and 0
We are asked to generate random numbers between -10 and 10
NOTE: rand function does not generate 0 and 1, it only generates the real numbers that fall between them