R
RayLopez99
If you think only I make rookie mistakes, check out this howler made
by Microsoft programmers.
When I've done a custom comparison I've always made sure the form was
proper. I even had fits over the "equality" operation; I forget the
issue, but it was critical that a value be returned that is logical.
Below, it seems the value returns depends on chance. I'm surprised it
did not crash, like the author below states.
RL
http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html
In this case Microsoft gave the following comparison function:
function RandomSort (a,b)
{
return (0.5 - Math.random());
}
Since Math.random() should return a random number chosen uniformly
between 0 and 1, the RandomSort() function will return a random value
between -0.5 and 0.5. If you know anything about sorting, you can see
the problem here. Sorting requires a self-consistent definition of
ordering. The following assertions must be true if sorting is to make
any sense at all:
If a<b then b>a
If a>b then b<a
If a=b then b=a
if a<b and b<c then a<c
If a>b and b>c then a>c
If a=b and b=c then a=c
All of these statements are violated by the Microsoft comparison
function. Since the comparison function returns random results, a
sort routine that depends on any of these logical implications would
receive inconsistent information regarding the progress of the sort.
Given that, the fact that the results were non-random is hardly
surprising. Depending on the exact search algorithm used, it may just
do a few exchanges operations and then prematurely stop. Or, it could
be worse. It could lead to an infinite loop.
by Microsoft programmers.
When I've done a custom comparison I've always made sure the form was
proper. I even had fits over the "equality" operation; I forget the
issue, but it was critical that a value be returned that is logical.
Below, it seems the value returns depends on chance. I'm surprised it
did not crash, like the author below states.
RL
http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html
In this case Microsoft gave the following comparison function:
function RandomSort (a,b)
{
return (0.5 - Math.random());
}
Since Math.random() should return a random number chosen uniformly
between 0 and 1, the RandomSort() function will return a random value
between -0.5 and 0.5. If you know anything about sorting, you can see
the problem here. Sorting requires a self-consistent definition of
ordering. The following assertions must be true if sorting is to make
any sense at all:
If a<b then b>a
If a>b then b<a
If a=b then b=a
if a<b and b<c then a<c
If a>b and b>c then a>c
If a=b and b=c then a=c
All of these statements are violated by the Microsoft comparison
function. Since the comparison function returns random results, a
sort routine that depends on any of these logical implications would
receive inconsistent information regarding the progress of the sort.
Given that, the fact that the results were non-random is hardly
surprising. Depending on the exact search algorithm used, it may just
do a few exchanges operations and then prematurely stop. Or, it could
be worse. It could lead to an infinite loop.