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!
