Sunday, April 30, 2017 06:47 am EDT

Randomly Shuffle an Array Using ActionScript 3

Bri's picture

If you have ever wanted to randomly re-arrange the elements of an array after they are already set, try using this function.


function randomizeArray(array:Array):Array
{
var newArray:Array = new Array();
while(array.length > 0){
var randomIndex = Math.floor(Math.random()*array.length);
newArray.push(array[randomIndex]);
array.splice(randomIndex, 1)
}
return newArray;
}

The function takes in the array that you want to be modified and returns a new one.

Comments

Anonymous's picture

Throws a warning. You forgot

Throws a warning. You forgot to specify the type of randomIndex (int)

Bri's picture

Oops

Thank you. I have not used ActionScript in a loooooong time. Thanks for the correction!