The Normal (or Gaussian) distribution is a frequently used distribution in statistics. While most programming languages provide a uniformly distributed random number generator, one can derive normally distributed random numbers from a uniform generator.



The task

 Take a uniform random number generator and create a large (you decide how large) set of numbers that follow a normal (Gaussian) distribution. Calculate the dataset's mean and standard deviation, and show a histogram of the data.
 Mention any native language support for the generation of normally distributed random numbers.


Reference

 You may refer to code in Statistics/Basic if available.


with javascript_semantics
procedure sample(integer n)
    -- show mean, standard deviation. Find max, min.
    sequence dat = repeat(0,n)
    for i=1 to n do
        dat[i] = sqrt(-2*log(rnd()))*cos(2*PI*rnd())
    end for
    printf(1,"%d data terms used.\n",{n})
 
    atom mean = sum(dat)/n,
         mx = max(dat),
         mn = min(dat),
         range = mx-mn
    printf(1,"Largest term was %g & smallest was %g\n",{mx,mn})
    printf(1,"Mean = %g\n",{mean})
    printf(1,"Stddev = %g\n",sqrt(sum(sq_mul(dat,dat))/n-mean*mean))
 
    -- show histogram
    integer nBins = 50
    sequence bins = repeat(0,nBins+1)
    for i=1 to n do
        integer bdx = floor((dat[i]-mn)/range*nBins)+1
        bins[bdx] += 1
    end for
    for b=1 to nBins do
        puts(1,repeat('#',floor(nBins*bins[b]/n*30))&"\n")
    end for
end procedure
 
sample(100000)
