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 played around with pseudorandom numbers and it works but is longer than simply calling
Math.random(). And it's not self contained, it needs to be called with an initial value for the generator, e.g.shuffle(a, new Date().getTime()). Without, it will create the same result every time.In the end I came up with a solution worth 108 Euro. No, not really. It's clearly a fake but it kind of shuffles all types of arrays. For example, the array
[1,2,3,4,5,6,7,8,9,10]becomes[2,7,8,3,9,6,4,10,1,5]. Looks random but isn't. It's the same every time. The function also accepts an initial value but since it's boolean there are only two possibilities how a specific array is ordered.