MATLAB: Generate random number between -1 and 1 with specified mean and variance

MATLABprobabilityrandomstatistics

Hi,
I need help generating a random number between -1 and 1 with a specified mean and variance.
I'm using c=a + b*randn() with 'a' as the mean and 'b' as the standard deviation but the problem is that since the randn function only generates a mean zero standard deviation 1 random variable then I might end up having c greater or less than 1.
Thanks

Best Answer

Hi, You did not state what distribution you want the random numbers to follow. From your use of randn() in your post, I'm assuming Gaussian. In that case you can scale randn() by a factor that will reduce the variance so that the numbers lies between [-1,1].
For example, since a Gaussian random variable is expected to be essentially the mean +/- 3*standard deviation (with probability close to 1), you can use
x = (1/3)*randn(100,1);
to generate a Gaussian-distributed random sample that essentially lies with +/- 1.
You can use rand():
r = -1 + 2.*rand(100,1);
to generate a uniformly-distributed random sample on [-1,1].
Wayne