Tushar’s Blog – BETA


Shuffling Array in Flash

Posted in Flash by Tushar Vaghela on December 10, 2008
Tags: , ,

We often need to shuffle array in flash but flash is not having any in-built function to shuffle the array.
Here is code to shuffle the array in a simple manner.

Please convert smart quotes to normal quote before pasting the following code to flash

// Initinal Value of the Array
var aData:Array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

trace(‘Before Shuffling : aData = {‘+aData+’}');// Before Shuffling : aData = {1,2,3,4,5,6,7,8,9,10}

for (i=0; i<aData.length; i++) {

//This will fetch the value at random index and save it in nData
var nData:Array = aData.splice(randRange(0, aData.length-1), 1);

//This will store the nData value to end of the same array
aData.push(nData[0]);
}

trace(‘After Shuffling : aData = {‘+aData+’}');// After Shuffling : aData = {5,6,1,3,7,10,2,9,8,4}

function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
return randomNum;
}

2 Responses to 'Shuffling Array in Flash'

Subscribe to comments with RSS or TrackBack to 'Shuffling Array in Flash'.

  1. Ritesh said,

    Good One, Keep the posting up man,

    Dont think who is reading and who is not. I strongly believe if the post is work for One Flash developer your efforts to
    post is 100% Returned.

    Keep It Up

  2. hhan said,

    nice trick..i will use it on my game ..thanks


Leave a Reply