author: rpenner [+], Submitted: 06.05.01 4a

msg1 { rpenner [+], posted: 04.06.01 11p•-, top [^] }
P.S. Thanks to Dickee for his meticulous testing, which gave true performance indicators and pointed out a critical flaw in one of my later implementations (now in the recycle bin :). Thanks also to Flashguru and LuxFX for their contributions to the shuffle wars at
http://board.flashkit.com/board/showthread.php?threadid=141081
msg2 { rpenner [+], posted: 04.06.01 11p•-, top [^] }
Two things:

1. If you want more speed, and don't care about deprecated syntax, use the old random() instead of Math.random().

2. It's best not to "var" variables multiple times, i.e. inside for loops--my oversight.

The code below has both of these optimizations.
By Oguzhan Eris [eris@Modstar.com] on Flashcoders.

Array.prototype.shuffle = function ()
{
var len = this.length;
var rand,temp,i;
for (i = 0; i<len; i++)
{
rand = random(len);
temp = this[i];
this[i] = this[rand];
this[rand] = temp;
}
}
msg3 { ralf@bokelberg.de [+], posted: 04.06.01 11p•-, top [^] }
supershort shuffling :)

function shuffle(a,b){
return random(2);
}

a = [1,2,3,4,5,6,7,8,9,0];

a.sort(shuffle);
trace(a);
msg4 { Dave_Yang@quantumwave.com [+], posted: 04.06.01 11p•-, top [^] }
Here another suggestion for a slightly faster shuffle:

Array.prototype.shuffle = function() {
var len = this.length;
var i = len;

while (i--) {
var p = random(len);
var t = this[i];
this[i] = this[p];
this[p] = t;
}
};

In terms of pre-defining vars outside of the loop, or as I've used above, I find that this is actually faster. I tested an array with 5000 elements, ran for 6 times and averaged the result.
msg5 { dev@tripleaxis.com [+], posted: 04.06.01 11p•-, top [^] }
Could someone please tell me how the hell that supershort shuffle proto works....

function shuffle(a,b){
return random(2);
}

It's gorgeous, I know how to use it, but must...know...secret....
msg6 { Dave_Yang@quantumwave.com [+], posted: 04.06.01 11p•-, top [^] }
Look up Array.sort(orderfunc)... if a function is passed as a parameter, it is used as a comparison function.
Here's what the doc says:

orderfunc An optional comparison function used to determine the sorting
order. Given the arguments A and B, the specified ordering function should
perform a sort as follows:
&#8226; -1 if A appears before B in the sorted sequence
&#8226; 0 if A = B
&#8226; 1 if A appears after B in the sorted sequence

By the way, I agree this method is nice and short, but is also very slow due to the built-in sort method. You could, of course, write your own sort method or pick one from this site.
msg7 { bzi [+], posted: 01.13.02 7a•-, top [^] }
slower, but shorter





:)
msg8 { bomberboy [+], posted: 09.11.02 7a•-, top [^] }
this prototype breaks XML parsing...

try this:

// myXML is a XML Object with 3 nodes
for(var i in myXML.childNodes){
trace(i);
}

output:
shuffle
2
1
0

not what I call usefull when parsing XML...
msg9 { Liam Boon [+], posted: 09.10.07 7a•09.10.07 7a, top [^] }
Hi, I realise this thread has been dead for a while!

I'm working on an XML/Flash gallery and need to shuffle an array of images (so there are no repeats within a series..), can someone show me how the examples above could be applied?

msg10 { kavvy [+], posted: 10.15.07 4a•-, top [^] }
Assuming you're loading a stack of image URL's from XML, and they are loaded sequentially into your array, you would just define & call the shuffle function:

e.g. Array.prototype.shuffle = function() {
var len = this.length;
var rand,temp,i;
for (i=0; i < len; i++) {
rand = Math.floor(Math.random()*len);
temp = this[i];
this[i] = this[rand];
this[rand] = temp;
}

yourImageArray.shuffle();

then you can pull the URL's from your array when needed, and they will be in random order.
i.e. myDisplay_mc.loadMovie(yourImageArray[index or var]);

Thats how i did it anyway, and it works fine :]