Weighted Random Number

Sumner, Walt WSUMNER at dom.wustl.edu
Mon Mar 4 12:06:56 EST 2013


This seems to work:

Make a stack with a button having this script, then click the button. It will bounce around in a normal distribution in H and V coordinates.


on mouseUp

   local tSD,tMean,tPoint,tStartPoint

   put the loc of me into tStartPoint

   put the width of this window div 6 & comma &  the height of this window div 6 into tSD

   put the width of this window div 2 & comma &  the height of this window div 2 into tMean

   put BoxMullerDist(tMean,tSD) into tPoint

   set the loc of me to tPoint

   wait 1 second

   set the loc of me to tStartPoint

end mouseUp


function BoxMullerDist pMean,pStdDev

   local tU,tV,tX,tY

   put random(100)/100 into tU

   put random(100)/100 into tV

   put sqrt(-2*ln(tU))*cos(2*pi*tV) into tX

   put sqrt(-2*ln(tU))*sin(2*pi*tV) into tY

   return item 1 of pMean + tX*item 1 of pStdDev & comma & item 2 of pMean + tY*item 2 of pStdDev

end BoxMullerDist

You can tweak the range by setting tSD to div 4 to hit the edges more often. You can tweak the center point by shifting the mean, but you will go off the edges sometimes if you shift too far.

Ref: http://en.wikipedia.org/wiki/Normal_distribution#Generating_values_from_normal_distribution

Walt Sumner



More information about the use-livecode mailing list