Skip to content
Advertisement

Is it correct to use JavaScript Array.sort() method for shuffling?

I was helping somebody out with his JavaScript code and my eyes were caught by a section that looked like that:

function randOrd(){
  return (Math.round(Math.random())-0.5);
}
coords.sort(randOrd);
alert(coords);

My first though was: hey, this can’t possibly work! But then I did some experimenting and found that it indeed at least seems to provide nicely randomized results.

Then I did some web search and almost at the top found an article from which this code was most ceartanly copied. Looked like a pretty respectable site and author…

But my gut feeling tells me, that this must be wrong. Especially as the sorting algorithm is not specified by ECMA standard. I think different sorting algoritms will result in different non-uniform shuffles. Some sorting algorithms may probably even loop infinitely…

But what do you think?

And as another question… how would I now go and measure how random the results of this shuffling technique are?

update: I did some measurements and posted the results below as one of the answers.

Advertisement

Answer

It’s never been my favourite way of shuffling, partly because it is implementation-specific as you say. In particular, I seem to remember that the standard library sorting from either Java or .NET (not sure which) can often detect if you end up with an inconsistent comparison between some elements (e.g. you first claim A < B and B < C, but then C < A).

It also ends up as a more complex (in terms of execution time) shuffle than you really need.

I prefer the shuffle algorithm which effectively partitions the collection into “shuffled” (at the start of the collection, initially empty) and “unshuffled” (the rest of the collection). At each step of the algorithm, pick a random unshuffled element (which could be the first one) and swap it with the first unshuffled element – then treat it as shuffled (i.e. mentally move the partition to include it).

This is O(n) and only requires n-1 calls to the random number generator, which is nice. It also produces a genuine shuffle – any element has a 1/n chance of ending up in each space, regardless of its original position (assuming a reasonable RNG). The sorted version approximates to an even distribution (assuming that the random number generator doesn’t pick the same value twice, which is highly unlikely if it’s returning random doubles) but I find it easier to reason about the shuffle version 🙂

This approach is called a Fisher-Yates shuffle.

I would regard it as a best practice to code up this shuffle once and reuse it everywhere you need to shuffle items. Then you don’t need to worry about sort implementations in terms of reliability or complexity. It’s only a few lines of code (which I won’t attempt in JavaScript!)

The Wikipedia article on shuffling (and in particular the shuffle algorithms section) talks about sorting a random projection – it’s worth reading the section on poor implementations of shuffling in general, so you know what to avoid.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement