Shuffle an array in-place using Fisher–Yates shuffle
var shuffle = function(a,b,c){for(b=a.length;b;c=0|b*Math.random(),a[c]=[a[--b],a[b]=a[c]][0]);}
var a = [1, 2, 3, 4, 5, 6, 7];
shuffle(a); console.log(a);
// [2, 3, 6, 7, 4, 1, 5]
shuffle(a); console.log(a);
// [3, 6, 5, 2, 1, 4, 7]
I'm afraid all of this will turn into an endless loop if called with e.g.
[1,0,2]
(or will fail as mentioned by @tsaniel above). Here is an other idea to save a byte, but this also shuffles nested arrays (except for the last one):function f(a){a.pop&&a.sort(f);return.5-Math.random()}