mob/verb/TestNormal()
var/list/l=list()
var/tot
var/k
var/mean //Possibly broken?
var/dev
var/amount=100
for(var/i=1,i<=amount,i++)
k=RandNormal(0,1)
tot+=k
l+=k
mean=tot/amount
tot=0
for(var/i in l) tot+=(mean-i)*(mean-i)
dev=sqrt(tot)/amount
world << "Mean [mean], deviation [dev]"
proc
RandNormal(mean=0,stddev=1) //Generates random numbers distributed normally around mean, with deviation stddev. Very slow.
ASSERT(stddev>0)
var/result
var/s2=stddev*stddev
var/cmult=1/(stddev*sqrt(2*pi))
var/p=0
var/factor=10000
do
result=rand((mean-3*stddev)*factor,(mean+3*stddev)*factor)/factor
p=cmult*e**(-(result-mean)*(result-mean)/(2*s2))
while(!prob(p))
return result
Problem description:
It doesn't work! :P
The mean reported by TestNormal() is about 0, so that's fine, but the standard deviation is an order of magnitude too low, or so. Is the algorithm wrong? Should I just use the Gaussian generator Lummox posted on BYONDscape? I don't really need the ability to change the standard deviation of the generated numbers - I was just trying to write a generalist proc.
Any ideas?