skip to content

Archive for the ‘ActionScript 3’ Category

Random permutation of array

Here is the code snippet for generating random permutation of array:

var a:Array = new Array( 1, 2, 3, 4, 5, 6, 7, 8, 9 );

trace( "Array: " + a );

for ( var i:uint = 0; i < a.length; ++i )
{
    var j:int = Math.random()*i;
    var j:int = Math.random()*( i + 1 );

    // Swap a[i] and a[j]
    var temp:* = a[j];
    a[j] = a[i];
    a[i] = temp;
}

trace( "Permutation: " + a );

Update: fixed bug with finding index of item to swap – thanks for pointing that out Kevin!

Seeded random number generator in ActionScript 3

Recently I needed seeded pseudo-random number generator for my Flex project. After searching I found three good articles with solution of that problem. Grant Skinner proposed use of BitmapData.noise() method. He also provides Parker-Miller psuedo-random number generator based on implementation by Michael Baczynski.

Useful links:

Displaying Sprite in Flex

Today I’ve tried to add simple Sprite instance to Flex Canvas using addChild() method, but it doesn’t work. Solution is very simple – use rawChildren.addChild().

⇥ Continued