[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: sorting by rand?

seebs

4/30/2007 8:35:00 AM

In message <200704300827.59213.swistak@mailx.expro.pl>, Marcin Raczkowski writes:
>On Monday 30 April 2007 02:55, Dan Zwell wrote:
>> > l = a.length
>> > a.each_index { |x| a[x] = a[rand(l)] }

>a.each_index { |x| r= rand(l); a[x], a[r] = a[r], a[x] }

Yeah. I can't even play the "only been using this language three
days" card, that was just plain dumb. It's examples like that that have
made unit testing the best thing that ever happened to me. I remembered
that the key component was putting a random thing in each spot...

Thinking about it, I think that Knuth proved that it doesn't matter
whether you select "any random element" or "any random element from the rest
of the list" when shuffling. At least, I *think* that was in ACP.
I could be wrong; I probably read it fifteen years ago.

-s