Arrays - Shuffle

I want to shuffle an array.

Listing 1


var original = [1, 2, 3, 4, 5];
original.sort(function(){return Math.random() - 0.5;});


Unfortunately, the above method does not produce a uniform distribution.

A different method

Listing 2


var original = [1, 2, 3, 4, 5], shuffled = [];

while (original.length) {
	shuffled.push(original.splice(Math.random() * original.length, 1));
}


This second method produces a uniform distribution.