From: Robert Dober Date: 2008-03-25T17:10:26+09:00 Subject: Re: generating unique random numbers On Tue, Mar 25, 2008 at 6:54 AM, Subbu wrote: > > On Mar 24, 6:05 pm, Tim Hunter wrote: > > Jimmy Palmer wrote: > > > I'm trying to generate 8 unique random numbers between 1 and 13. > > > > > for example my first set of results could be: > > > > > 2, 8, 4, 6, 3, 10, 12, 1 > > > > > the results need to be between 1 and 13 and they must be unique. > > > > > The rand(12) + 1 returns random numbers between 1 and 13, but they are > > > not unique. Any quick solutions? > > > > ~$ irb > > irb(main):001:0> (1..13).to_a.sort_by{rand}[0..7] > > => [7, 3, 8, 2, 11, 4, 1, 9] > > irb(main):002:0> > > > > -- > > RMagick:http://rmagick.rubyforge.org/ > > RMagick 2:http://rmagick.rubyforge.org/rmagick2.html > > Hey Tim, I am a bit lost here :) sort_by accepts a block. But I see > you are just passing it a method. Look again :) Robert -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein