Generating values from normal distribution:
An easy to program approximate approach, that relies on the central limit theorem, is as follows: generate 12 uniform U(0,1) deviates, add them all up, and subtract 6 — the resulting random variable will have approximately standard normal distribution. In truth, the distribution will be Irwin–Hall, which is a 12-section eleventh-order polynomial approximation to the normal distribution. This random deviate will have a limited range of (-6, 6).
NormalRand(x,y,int=1) { ;x lower y upper int for integer return
Loop 12
{
Random, var,0.0,1
Num+=var
}
norm := (int) ? Round((y+x)/2+((Num-6)*(y-x))/6) : (y+x)/2+((Num-6)*(y-x))/6
Return norm < x ? x : norm > y ? y : norm
}
MsgBox % NormalRand(0,10) ;random integer between 0 and 10
MsgBox % NormalRand(5,20,0) ;random float between 5 and 20
I thought a Graphic representation of the difference might helpthe first is just random numbers (every point in the range has same probability) and the second has a normal distribution (points further from the center less likely)
SetMouseDelay, -1 ;this code generated that picture by having it click in paint
SetBatchLines, -1
#a::
MouseGetPos, x, y
Loop 5000
Click % Rand(x-170,x-10) ", " Rand(y-80,y+80)
Loop 5000
Click % NormalRand(x+10,x+170) ", " NormalRand(y-80,y+80)
Return
NormalRand(x,y,int=1) { ;x lower y upper int for integer return
Loop 12
{
Random, var,0.0,1
Num+=var
}
norm := (int) ? Round((y+x)/2+((Num-6)*(y-x))/6) : (y+x)/2+((Num-6)*(y-x))/6
Return norm < x ? x : norm > y ? y : norm
}
Rand(x,y) { ;just a random wrappr
Random, var,%x%,%y%
Return var
}Edit: added picture and more example code
Edit: added some code to prevent it from returning values outside the range




