From: "M. Edward (Ed) Borasky" Date: 2007-04-30T15:10:54+09:00 Subject: Re: sorting by rand? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Peter Seebach wrote: > Browsing something at a bookstore recently, I saw an example of > an array shuffle allegedly implemented with > x.sort { rand } > or something to that effect. > > This seems sort of unlikely, to me, to be a correct shuffling > algorithm. I'm not even sure it's safe at all. I know that > doing the same thing to C qsort implementations occasionally > causes one to blow up spectacularly, because its assumption > that the comparison between any two items is constant gets > broken. > > So, two vaguely related questions: > 1. Is there a reasonably sane way to do this, should I ever find > myself wanting one? > 2. Am I right in guessing that handing garbage to Array#sort > is probably crazy? > > -s > > Welll ... I do this in Excel all the time -- add a column of random numbers to the right of the (CSV) file I want to shuffle, sort it about five times, hitting F9 before each sort to change the random numbers, and then deleting the random column and saving the file. Translate that to Ruby and I think you've got it. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.3 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGNYhUaZUb+jwczfoRAgn3AKDJnG8yQYVTWkoPTw9YNT42/6M/OACg1NFg 76A4qzYkZjIIKBA/YAPblDM= =KVsa -----END PGP SIGNATURE-----