MATLAB: Randn function with so many digits or huge numbers

MATLABrandn

Hallo my question is about generating normal distributed random numbers with randn function of MATLAB, e.g.
x=25.589665545.*(1+0*randn(100,1));
std(x)
ans =
1.7853e-014
or with huge numbers :
y=10^25.*(1+0*randn(10000,1));
std(y)
ans =1.0631e+012
is that not strange? to be sure there are some other ways to generate array with constant numbers, but I see this when I want to generate variables with different deviations, for any command pthanks in advance

Best Answer

Metin - the result is not strange for the first example, since your vector of 100 elements is all ones (due to the 0*randn) and once multiplied by the scalars, then all 100 elements will be 25.589665545…and so the standard deviation will be 0 (or close to it).
The second example is odd - I would expect the same result as the first since all numbers in the array are identical and so the standard deviation of that vector should (once again) be zero or close to it. And it isn't! Running that on my version of MATLAB, I get the same result as yours. Note that this may be an example of arithmetic overflow. If I do:
std(y(1:33));
the result is zero (as expected). If I increase this to
std(y(1:34));
the answer is 2179778447.36383. Which is unexpected since the first 34 elements are identical. Shifting the standard deviation calculation to:
std(y(2:34));
once again returns a zero. So there isn't a problem with y(34) and it seems that the standard deviation of any 33 elements is fine - it is just once you add one or more to that list, then the overflow occurs. See standard deviation equation used in MATLAB for details.
Geoff