Gaussian pseudo-random numbers -- math issues

Mark Smith lists at
Wed Oct 22 21:46:14 EDT 2008

Timothy, the only way I could find of doing what you want is called  
'the polar form of the Box-Muller transformation'.

Fortunately, it's not as bad as it sounds. :)

I've copied an ADA implementation, and here it is:

function polarBoxMuller pLength, pMean, pSdev
    put false into useLast
    repeat pLength

          put ((random(101) - 1) * 0.02) - 1 into x1 -- generate a  
random number between -1 and +1
          put ((random(101) - 1) * 0.02) - 1 into x2

          put (x1 * x1) + (x2 * x2) into w
          if w <= 1 then exit repeat
       end repeat

       if w <> 0 then put ln(w) * -2 into w -- rev barfs if w is zero
       if w <> 0 then put sqrt(w) / w into w -- ditto

       put x1 * w into y1
       put x2 * w into y2

       put pMean + y1 * pSdev & comma after tList
       put pMean + y2 * pSdev & comma after tList
    end repeat

    return char 1 to -2 of tList
end polarBoxMuller

This should give you a comma delimited list of (fairly) normally  
distributed numbers.



ps. I enjoy this sort of thing, so thanks for the question!

On 21 Oct 2008, at 23:03, Timothy Miller wrote:

> Greetings,
> I'm interested an a modest statistics demonstration, but I can't  
> figure out how do to the math myself.
> I'd like to have a few lines of code that produces a sequence of  
> numbers. (whole numbers would probably be okay). I'd like to  
> specify the number of numbers generated. Let's call that Z.
> I'd like also to specify the desired mean and standard deviation.  
> I'd like the function (is this a function??) to work in such a way  
> that if Z is large, the set of numbers generated, if graphed as a  
> frequency distribution, would be normally distributed, i.e., Gaussian.
> If Z is rather small, then the mean and standard deviation of the  
> numbers produced will would only approximate the desired mean and  
> standard deviation. Different runs would produce different actual  
> means and standard deviations.
> If Z is very small, like 3 or 4, the numbers will look almost random.
> I hope I explained that clearly.
> Optionally, I might also be able to enter a variable that would  
> specify the desired number of digits to the right of the decimal  
> point.
> No favors are requested. I'd really be rather uncomfortable with a  
> generous gesture. However, if someone has some code like this  
> sitting around, and you're willing to share it, with a few notes  
> about how to use it, I'd appreciate it. If not, it can't be helped.
> Hmmmm... I wonder if some website somewhere would do the work for  
> me. That could work... I looked around, but didn't find anything.
> Thanks in advance.
> Tim Miller
> _______________________________________________
> use-revolution mailing list
> use-revolution at
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:

More information about the Use-livecode mailing list