A
ara.t.howard
od.=2E
I did some numbers and found the following probability estimates:
array size P(#rand() collision)
1000 5.54558e-11
10000 5.55056e-09
1000000 5.55096e-05
10000000 5.53574e-03
A collision implies that the associated pair of elements has got the same
ordering in the output array, instead of 50-50 chances of it being revers= ed.
More info, including the Ruby code I used, available at
http://eigenclass.org/hiki.rb?sort_by+rand+is+biased
I'm not totally sure it's really unbiased, but at first sight it looks go=
the big difference is that a value will have rand called multiple times for=
it
- in otherwords an element compareds differently every time. this is bound=
to
help shuffle more uniformly it seems:
harp:~ > cat a.rb
%w( a b c d ).sort{|a,b| ra =3D rand; rb =3D rand; p a =3D> ra; p b =3D>=
rb; ra <=3D> rb }
harp:~ > ruby a.rb
{"a"=3D>0.181439380218727}
{"c"=3D>0.579403886629126}
{"c"=3D>0.713513989939958}
{"d"=3D>0.573687508540169}
{"a"=3D>0.851534740906118}
{"d"=3D>0.00156074151427565}
{"b"=3D>0.803591007691647}
{"a"=3D>0.494066110872563}
{"b"=3D>0.834676789626288}
{"c"=3D>0.792106134460754}
so, note that "a" was sorted using a different value each time.
can anyone who remembers stats and knows the sort algorithm weigh in?
-a
--=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D