From: lasitha Date: 2008-03-27T14:19:15+09:00 Subject: Re: generating unique random numbers Tim Hunter wrote: > Jimmy Palmer wrote: > > I'm trying to generate 8 unique random numbers ... > > the results need to be between 1 and 13 > > and they must be unique. > > ~$ irb > irb(main):001:0> (1..13).to_a.sort_by{rand}[0..7] > => [7, 3, 8, 2, 11, 4, 1, 9] For anyone still counting :), the following builds on Tim's approach: (1..13).to_a.shuffle.first(8) Array#shuffle is only available in ruby 1.9+ though. Cheers, lasitha -- View this message in context: http://www.nabble.com/generating-unique-random-numbers-tp16266148p16322324.html Sent from the ruby-talk mailing list archive at Nabble.com.