The following algorithm computes s2 in one pass while minimizing the possibility that accumulated numerical error produces erroneous results: s1 = 0; run the simulation for the first time, producing result r1; m1 = r1; //mi is the mean of the first i simulations for i = 2 to k do run the simulation for the ith time, producing result ri; δi = ri - mi−1; mi = mi-1 + (1/i)δi; si = si-1 + ((i - 1)/i)(δi)2; repeat s2 = sk/(k - 1); Note that, when the algorithm completes, mk is the mean of the k results.
The following algorithm computes s2 in one pass while minimizing the possibility that accumulated numerical error produces erroneous results: s1 = 0; run the simulation for the first time, producing result r1; m1 = r1; //mi is the mean of the first i simulations for i = 2 to k do run the simulation for the ith time producing result ri; δi = ri - mi−1; mi = mi-1 + (1/i)δi; si = si-1 + ((i - 1)/i)(δi)2; repeat s2 = sk/(k - 1); Note that, when the algorithm completes, mk is the mean of the k results.