You can see the problem I’m describing very vividly using Firefox in Mike Bostock’s visualization for array shuffling methods. Other helpful links below.
I’ve just helped another software developer (not to be named) fix a debilitating problem with naive shuffle code depending on JavaScript sort(function()). Some smart-aleck popularized a cute but irresponsible technique for shuffling arrays as follows:
array.sort(function() { return Math.random() - .5 }
This lazy random comparator technique is all over the Internet. Worse, Firefox’s sort() calls this function in an implementation-specific way that makes the resulting array very non-random. Everyone recommends Fisher-Yates as a solution, and of course that’s true, but that does nothing to address the fact of hundreds of thousands of sites that haven’t woken up and still use this bad code. That points not to these coders but to Firefox’s implementation of sort().
Someone should have a close look at the difference between Firefox’s sort() comparator code and how other browsers do it. None of them is perfect, but try Bostock’s little visualization on Chrome and it will be like night and day: Chrome gives a pretty clean swap using the identical code. Why? Why can’t this be fixed in Firefox instead of people having to discover and repair the “Math.random() -.5” comparator code implementations? Why only search for land mines, when you can deactivate most of them with a few lines of code in Firefox? Both fixes are good ideas, but what about fixing Firefox so its code also contributes to solving this problem?
www.linuxscrew.com/javascript-randomize-shuffle-array
bost.ocks.org/mike/shuffle/compare.html
en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm