News for hackers. Stuff that matters.
Re:Advanced Javascript tips and tricks by yid on Tuesday July 05, 2011 @03:17AM (#2728963)
While I agree in principle, there are many cases where you don't care about the statistical quality of a random shuffle, where brevity has its benefit.
Re:Advanced Javascript tips and tricks by bzbarsky on Tuesday July 05, 2011 @11:02AM (#2730061)
You don't understant. The code cited isn't even guaranteed to terminate by the spec; browsers could just go into an infinite loop if the comparison function effectively lies to them so the array never looks sorted... and this would be perfectly reasonable behavior.

So it's not that you get a bad random shuffle; it's that your code might never return from that sort() call.

And to be clear, there have been cases of "hang" bugs being reported to browser vendors in situations exactly like this one because people were writing boneheaded script like this.

Re:Advanced Javascript tips and tricks by photon_off on Tuesday July 05, 2011 @03:24AM (#2728977)
I think there's also an issue of performance -- it's possible, though unlikely, that the sort could take forever to complete.
Re:Advanced Javascript tips and tricks by yid on Tuesday July 05, 2011 @03:30AM (#2728991)
Unlikely is an understatement -- the probability of an infinite sequence of PRNG outputs that prevents any sort algorithm from terminating is negligible.
Re:Advanced Javascript tips and tricks by wlievens on Tuesday July 05, 2011 @05:28AM (#2729187)
What about 'taking too much time' though?
Re:Advanced Javascript tips and tricks by yid on Tuesday July 05, 2011 @11:29AM (#2730181)
Touche...
Re:Advanced Javascript tips and tricks by tolmasky on Wednesday July 06, 2011 @06:02AM (#2733197)
When? It's not like anyone is asking you to write the shuffling code by hand, so when is time so precious, and space so constrained, that it's beneficial to use this "brief" incorrect solution over taking the 5 minutes to google a correct solution and copy-pasting that?