use-revolution Digest, Vol 61, Issue 36

Mick Collins mickclns at mac.com
Thu Oct 23 01:21:09 EDT 2008


Hi, Timothy,
Here are a couple of functions that I think will fit the bill for  
you.  The first returns a number in standard normal distribution  
(mean 0 and standard deviation 1).  The second returns a number in  
normal distribution with mean mu and standard deviation sigma (as  
arguments.

They are followed by handlers to print out a histogram for 10000 numbers


function stdNormDist
    constant fourG = 4000000000
    return sqrt(-2*ln(1-random(fourG)/ fourG))*cos(random(fourG)/  
fourG *2*pi)
end stdNormDist


function normDist mu, sigma
    return sigma * stdNormDist() + mu
end normDist



on tst
    put 10 into sigma
    put 100 into mu
    repeat with i = 0 to 32
       put 0 into hist[i]
    end repeat

    repeat with i = 1 to 10000
       put normDist(mu, sigma)  into nD
       put (nD - mu) / sigma into nSDs
       put max(0,min(32,round(nSDs*4)+16)) into ndx -- 4 slots per  
std dev
       add 1 to hist[ndx]
    end repeat
    put printHistLine(hist[0])  into printTmp
    repeat with i =1 to 32
       put return & printHistLine(hist[i]) after printTmp
    end repeat
    put printTmp
end tst

function printHistLine histCount
    put round(histCount/25) into hCount
    return "|" & multiStr("x", hCount) && histCount
end printHistLine

function multiStr str, n
    put empty into tmp
    repeat n times
       put str after tmp
    end repeat
    return tmp
end multiStr


| 0
| 3
| 2
| 6
| 5
|x 28
|xx 48
|xxxx 88
|xxxxxx 140
|xxxxxxxxx 228
|xxxxxxxxxxxxxx 345
|xxxxxxxxxxxxxxxxxx 447
|xxxxxxxxxxxxxxxxxxxxxxxx 596
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 749
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 868
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 951
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 999
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 964
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 910
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 775
|xxxxxxxxxxxxxxxxxxxxxxxx 591
|xxxxxxxxxxxxxxxxxx 461
|xxxxxxxxxxxx 291
|xxxxxxxx 192
|xxxxxx 143
|xxx 84
|xx 41
|x 28
| 12
| 4
| 1
| 0
| 0


> From: Timothy Miller <gandalf at doctorTimothyMiller.com>
> Subject: Gaussian pseudo-random numbers -- math issues





More information about the use-livecode mailing list