MATLAB: Random numbers with Zero mean (not the basics)

iterationsrandnzero mean

Hello, I have been searching for an alternative for my problem, but wasn't really able to get a convenient solution.
Issue with randn:
Although randn is based on zero mean, it doesn't really produce an array with zero mean. Even if I generate 1 million random variables from the standard normal, the mean sometimes is "far" from zero (ex: 0.003). I really need zero mean generations. I can't simply deduct the mean, as I need to use the numbers generated in simulating new stock prices, this moves me to my specific issue.
Issue with my case:
To simplify it to the maximum, please consider the following algorithm:
for i=1:j
Step 1 StockPrice %Given
Step 2 %Some calculations

Step 3 StockPrice = StockPrice*randn %Generating new stock price

Step 4 %Some calculations
Step 5 StockPrice = StockPrice*randn %Generating new stock price
end
I really don't want to complicate my code with if statements to achieve the zero mean. Kindly note that generating one vector of random numbers will be much faster than my current method.
My problem will be solved if there is a way to move in a vector in every iteration.
Thanks in advance

Best Answer

AND, no offense intended, but this doesn't make a lot of sense to me.
I suspect you know the following: randn draws from a standard normal distribution, which has zero mean. Any finite set of independent values drawn from that distribution will, with probability 1, have a non-zero sample mean. But that's their sample mean, not the mean of the distribution that they are drawn from. This is exactly what you should expect when drawing independent values from a normal distribution. It is not in any sense a "strange behavior".
You probably also know most of the following: If you want a finite set of "normal-like" values that has zero mean, then the simplest thing is to generate however many values you need, and subtract their mean. Another possibility is what's called antithetic sampling, where for each value that you draw from randn, you also use its negative. You can get that using the Antithetic property of the gloabl RandStream object. Both of these create what are most definitely not independent draws from a normal distribution. They are constrained dependent draws.
You say you cannot do that, and that you need "a method that allows me to use them efficiently in the loop". I'm going to have to guess that the reason is one of two things:
1) You are generating your values one at a time. I won't get into the efficiency issues that that raises, but unless you're generating hundreds of millions of values, you should be able to call randn once to generate a vector of however many values you need, subtract off its mean, and draw from that vector in your loop. Equivalently, you could read and save the generator state, generate a large vector and save its mean, then reset the generator state and draw numbers one at a time, subtracting off the mean that you previously saved.
2) You don't know in advance how many loop iterations you will have. If that's the case, then I think you are asking for the impossible. The only way that you can expect to draw random values and maintain a sample mean of zero at all steps is to draw only zeros.
You have not said why you care about a zero mean. Perhaps that would make things more clear.