randNormal with incorrect stddev

Run some MiniScript code right in your browser!

randNormal = function(mean=0, stddev=1) u = rnd v = rnd // Box-Muller method: return mean + u * sqrt(-2 * log(u,2.7182818284)) * cos(2*pi*v) * stddev end function list.mean = function() sum = 0 for x in self sum = sum + x end for return sum / self.len end function list.stddev = function() sum = 0 sumOfSquares = 0 for x in self sum = sum + x sumOfSquares = sumOfSquares + x*x end for mean = sum / self.len // Note: this is NOT including Bessel's correction return sqrt(sumOfSquares / self.len - mean*mean) end function gold = [4,2,5,8,6] // correct answer (without Bessel's correction): 2 print "gold mean: " + gold.mean + " stddev: " + gold.stddev samples = [2,4,4,4,5,5,7,9] // correct answer (without Bessel's correction): 2 print "samples mean: " + samples.stddev + " stddev: " + samples.stddev x = [0] * 1000 for i in x.indexes x[i] = randNormal end for print "Mean: " + x.mean // should be about 0 print "Stddev: " + x.stddev // should be about 1

Help & Information