coder and technology lover
Implementing Array.shuffle() in Actionscript
One useful thing that Actionscript doesn’t offers is the ability to shuffle an array, however this can be accomplished in a blink of an eye and with very few lines of code. Before to implement my own solution, I looked on the web, but solutions provided by others developers looks too complex and too long to type. What I found was based on a for loop and several line of codes to fill a secondary array with the elements in a new random order. My solution instead is based on a while loop (3 lines of code only):
var arr2:Array = [];
while (arr.length > 0) {
arr2.push(arr.splice(Math.round(Math.random() * (arr.length - 1)), 1)[0]);
}
How it works?
First we have to create a new array (arr2) which will contains the elements from the base ordered array (arr), then we use a while loop, which can be translated into english as: “do this until base array is not empty”. Inside the loop, we push into the second array a random element from the first array and, at the same time, we remove that element from its array. Array’s method splice() in fact, removes one or more elements from an array and returns an array containing the deleted elements, that in our case is only one (as specified in the second argument). The first argument of the method represent the starting index from which to start to delete, this is dynamically calculated by using the Math.random() method (which returns a number between 0 and 1), then multiplied by the length of the base array minus 1, because array length is always 1 greater than elements array contains (cause array indexing starts from zero), finally this number is rounded to the nearest integer by Math.round(), in order to access an exact index.
I think this is the best and shorter way to implement a shuffle functionality :)


24, Tuesday f, 2009 - 2:23 am
Thanks David, it’s simple, it’s short, it’s elegant and it works; as opposed to many other ’sollutions’ I found on the webs.
24, Tuesday f, 2009 - 3:33 am
You are welcome Marc ;)
19, Sunday f, 2009 - 1:02 pm
I actually arrived at this exact same solution through trial and error, and just wanted to see if anyone had arrived at anything better. IMO this is all you need. Cheers…
19, Tuesday f, 2009 - 5:47 am
Acctually your solution, although short code-wise, is approximately twice as slow as e.g. this:
function doRandArray7(a:Array):Array {
var nLen:Number = a.length;
var aRand:Array = a.slice();
var nRand:Number;
var oTemp:Object;
for (var i:Number = 0; i < nLen; i++) {
oTemp = aRand[i];
nRand = i + (random(nLen – i));
aRand[i] = aRand[nRand];
aRand[nRand] = oTemp;
}
return aRand;
}
19, Tuesday f, 2009 - 5:58 am
I haven’t tried your code, but I’m sure you are right. Anyway I’m not saying my solution is the fastest but sure the shorter, each developer is free to choose the approach he prefers.
My only goal was to show an alternative and really simplified technique to implement a shuffle() method ;)
19, Friday f, 2009 - 7:38 am
Thanks this worked very well. Just what I was looking for, something simple.
23, Tuesday f, 2009 - 11:19 am
Thanks Dave :)
10, Monday f, 2009 - 8:13 am
Hi!
I don’t understand where that arr thing is coming from.
“which will contains the elements from the base ordered array” – Could you explain what you mean? Please help me! The code looks so easy :D
10, Monday f, 2009 - 9:20 am
Hi Mark, that “arr” is an argument (the original array to shuffle), imagine a function like this:
public function shuffle(arr:Array):Array {
var arr2:Array = [];
while (arr.length > 0) {
arr2.push(arr.splice(Math.round(Math.random() * (arr.length – 1)), 1)[0]);
}
return arr2;
}
Is it clear now?
17, Thursday f, 2009 - 6:53 am
Yes, thank you very much!
18, Friday f, 2009 - 4:16 am
Hey Dave,
Thanks for the AS3 post & comments on how to implement the shuffle code,
nice work!
btw, I enjoy using Arduino Board (from Italy) to create projects that cross code, design & art – might be something you’re interested in? – check my site if you are curious.
Cheers mate,
/Rich
12, Monday f, 2009 - 12:59 am
Hi Dave, thank you very much for this very smart code.
I had tried another solution by MRSTEEL, that required much more code, and was focusing on execution speed. But testing it on a small array (23 items), I realized the randomness seemed very poor: the first item of the original array seemed to remain in the first position half of the time, and seemed to never make it further than fourth position.
Your code doesn’t seem to suffer from the same defect.
So as far as I’m concerned, your code is very satisfying in terms of lines spared, and quality of randomness.
12, Monday f, 2009 - 1:23 am
@deesnay: Thank you so much ;-)
24, Tuesday f, 2009 - 4:46 pm
hey Dave thanks alot. I appreciate your code and explanation. Im new to action script and i wasnt looking forward to spending a few hours trying to figure this out. All the other examples i read were too complex. 3 lines of code…you can beat that really. Thanks Alot!
14, Monday f, 2009 - 7:23 am
Thank you!