Chris Herring

 Flash Developer

 London

Shuffle Shuffle Array

21/07/09

Some projects you find yourself needing to shuffle arrays to randomise the objects within, below is a little static function for Actionscript 3 which will do the above. In order to use the function you need to do the following…

toBeShuffledArray = ArrayShuffle.shuffle(toBeShuffledArray);


public class ArrayShuffle()
{
public static function shuffle(array:Array):Array
{
var obj:*;
var ran:Number;

for(var i:Number = 0; i {
obj = array[i];
ran = Math.floor(Math.random() * array.length);
array[i] = array[ran];
array[ran] = obj;
}
return array;
}

}

Source code

Tags: , , ,

No comments

 

Post a comment