MATLAB: Problem 44951. Verify Law of Large Numbers

doubtMATLAB

function dice_diff = loln(N)
add = 0; mean = 0; mean1 = 0;add1=0;
for c=1:1*exp(8)
num = randi(N);
add = add + num;
end
mean = add/(1*exp(8))
for c=1:N
add1=add1+c;
end
mean1=add1/N
dice_diff = mean-mean1
end
The code works fine but the result is not as expected. Could anyone tell the reason?

Best Answer

  • If you want 2980 iterations then why not use 2980 instead of exp(8) ?
    mean = add/(1*exp(8))
    Incorrect. You do an integer number of iterations, 2980, but you divide by exp(8) which is about 2980.96 so you get the wrong mean() value. You sould be dividing by the number of iterations you do. Or you should use mean()