Proposal: Array.prototype.shuffle (Fisher-Yates)

John Terenzio john at
Thu May 16 14:02:59 PDT 2013

Many scripting languages, such as Ruby, PHP, and Python (via the random
module), have a built-in for properly shuffling an array. I propose adding
Array.prototype.shuffle to the ES spec. I think it should act like
Array.prototype.reverse, shuffling the array in place and returning a
reference to it. It should use the Fisher-Yates algorithm. See

Since this functionality is missing, some less-seasoned developers fall
into the trap of using the "sort by random" method, which produces bias.

In a perfect world, this would use a better random number generator than
the existing Math.random, but for now an implementation like the one below
would still be a vast improvement over having to implement manually and
possibly incorrectly. See

Example implementation:

I don't think adding this to the spec would cause many compatibility issues
and the implementation should be very straightforward. Let me know what you

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list