Views
Box Car Averager
From Shadowfax
ActionScript 3.0 Boxcar Averager
This is one I knocked up when I couldn't sleep. A little ActionScript application to analyse the outputs of the random number generator running a few different simulations. Click on the app and hit spacebar to cycle through the equations.
There are 100 bars (from 0-99) and each one increases in height by one pixel every time that number is rolled by the random number generator.
The ActionScript is presented below.
// Box car averager package { import flash.display.Shader; import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.text.TextField; import flash.text.StyleSheet; import flash.events.KeyboardEvent; public class Main extends Sprite { private var shArr:Array = new Array(); private var hArr:Array = new Array(); private var tf:TextField = new TextField(); private var eqn:int = 0; private var yAxis:int = 480; private var xWidth:int = 710; private var barSpace:int = 7; private var barWidth:int = 6; private var xOffset:int = 2; public function Main():void { var sh:Shape = new Shape(); for (var i:int = 0; i<=100; i++) { shArr[i] = new Shape(); hArr[i] = 0; addChild(shArr[i]); graphics.beginFill(0x006600); graphics.drawRect(i*barSpace+xOffset, yAxis, barWidth, 1); graphics.endFill(); } for (i = 0; i < yAxis; i += 20) { sh.graphics.lineStyle(1, 0x006600); sh.graphics.moveTo(0, yAxis - i); sh.graphics.lineTo(xWidth, yAxis - i); } tf.x = xOffset; tf.y = yAxis + 2; tf.width = xWidth; var tSS:StyleSheet = new StyleSheet(); tSS.parseCSS('p {font-family: sans-serif;font-size:11pt; color: #00cc00;}'); tf.styleSheet = tSS; tf.htmlText = "<p>Box Car Averager. 2x(Rnd(6)xRnd(6)) - (Hit Spacebar for next)</p>" sh.alpha = .2; addChild(sh); addChild(tf); addEventListener(Event.ENTER_FRAME, onEnterFrame); stage.addEventListener(KeyboardEvent.KEY_DOWN, trapKeyDown); } private function dice (sides:Number, startAtOne:Boolean = true):int { if (startAtOne) return int(Math.floor(Math.random() * sides) +1); else return int(Math.floor(Math.random() * sides)); } private function onEnterFrame(e:Event):void { var i:int; var n:Number; switch(eqn) { case 0: n = (Math.random() * 6) * (Math.random() * 6); i = Math.floor(n * 2); break; case 1: i = dice(6) * dice(6); break; case 2: i = dice(6) + dice(6); break; case 3: i = dice(6) + dice(6) + dice(6); break; case 4: i = dice(10) + dice(10); break; case 5: i = (dice(10,false)*10) + dice(10,false); break; } hArr[i]+=1; shArr[i].graphics.beginFill(0x009900); shArr[i].graphics.drawRect(i*barSpace+xOffset, yAxis-hArr[i], barWidth, hArr[i]); shArr[i].graphics.endFill(); } private function trapKeyDown(e:KeyboardEvent):void { if (e.keyCode == 32) { eqn++; if (eqn > 5) eqn = 0; switch(eqn) { case 0: tf.htmlText = "<p>Box Car Averager. 2x(Rnd(6)xRnd(6)) - (Hit Spacebar for next)</p>" break; case 1: tf.htmlText = "<p>Box Car Averager. Two D6 (multiplied) - (Hit Spacebar for next)</p>" break; case 2: tf.htmlText = "<p>Box Car Averager. Two D6 (added) - (Hit Spacebar for next)</p>" break; case 3: tf.htmlText = "<p>Box Car Averager. Three D6 (added) - (Hit Spacebar for next)</p>" break; case 4: tf.htmlText = "<p>Box Car Averager. Two D10 (added) - (Hit Spacebar for next)</p>" break; case 5: tf.htmlText = "<p>Box Car Averager. Percentage Dice - Two D10 multuplied- (Hit Spacebar for next)</p>" break; } for (var i:int = 0; i<=100; i++) { shArr[i].graphics.clear(); hArr[i] = 0; } } } } }
Leave your comment