In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2011-1-27

Get Adobe Flash player
by yonatan 27 Jan 2011
    Embed
/**
 * Copyright yonatan ( http://wonderfl.net/user/yonatan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ucQk
 */

package {
    import flash.display.*;

    public class FlashTest extends Sprite {
        public function FlashTest() {
            var uniformDist:Shape = visualize(uniformRnd, 20000);
            addChild(uniformDist);

            var gaussDist:Shape = visualize(gaussRnd, 20000);
            gaussDist.y = 220;
            addChild(gaussDist);
        }

        // Uniform random number between -1 and 1
        private function uniformRnd():Number { return Math.random()*2-1 }

        // Normal (gaussian) random number,
        // Box-Muller method, loosely based on code from Numerical Recipes - 
        // http://apps.nrbook.com/c/index.html - pages 289-290
        private function gaussRnd():Number {
            var stddev:Number = 0.5; // standard deviation
            var u:Number, v:Number, rsq:Number, fac:Number;

            // pick uniform random (u,v) inside the unit circle
            do {
                u = uniformRnd();
                v = uniformRnd();
                rsq = u*u + v*v;
            } while(rsq >= 1 || rsq == 0);

            return stddev * Math.sqrt(-2.0 * Math.log(rsq) / rsq) * u; // v can be swapped for u
        }

        private function visualize(rnd:Function, cnt:int):Shape {
            var canvas:Shape = new Shape;
            var bins:Vector.<Number> = new Vector.<Number>(200);
            
            while(cnt--) {
                var idx:int = rnd()*100+100;
                if(idx >= 0 && idx < 200) { // normal random can be out of bounds
                    bins[idx]++;
                }
            }

            canvas.graphics.clear();
            canvas.graphics.beginFill(0x808080);
            for(var i:int = 0; i < 200; i++) canvas.graphics.drawRect(i*2, 200-bins[i], 2, bins[i]);
            canvas.graphics.endFill();
            return canvas;
        }
    }
}