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!

February 2nd, 2009 at 10:03 am
I don’t think your code generates random permutations. I believe you need to change the line:
var j:int = Math.random() * i;
to:
var j:int = Math.random() * (i+1);
Otherwise, the last element of the array can never end up in its original position.
May 4th, 2009 at 3:14 am
what is the operator * in var temp:*