Re: sorting by rand?
- From: seebs@xxxxxxxxx (Peter Seebach)
- Date: Mon, 30 Apr 2007 17:34:30 +0900
In message <200704300827.59213.swistak@xxxxxxxxxxxxxx>, 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
.
- References:
- Re: sorting by rand?
- From: Marcin Raczkowski
- Re: sorting by rand?
- Prev by Date: Re: sorting by rand?
- Next by Date: How To Delete An Email's Attachment?
- Previous by thread: Re: sorting by rand?
- Next by thread: Re: sorting by rand?
- Index(es):