From: Jeremy Hinegardner Date: 2008-03-25T13:52:42+09:00 Subject: Re: generating unique random numbers On Tue, Mar 25, 2008 at 10:24:54AM +0900, Chris Shea wrote: > On Mar 24, 7:00 pm, 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? > > -- > > Posted viahttp://www.ruby-forum.com/. > > Here's one more possibility: > > numbers = (1..13).to_a > randoms = [] > 8.times {randoms << numbers.delete_at(rand(numbers.size))} > > Though, Tim's solution might be most readable. You might also look through the solutions for Ruby Quiz #39 (Sampling). http://rubyquiz.com/quiz39.html there are couple of other ruby quizes where things like this are touched on. You might browse through the archives. enjoy, -jeremy -- ======================================================================== Jeremy Hinegardner jeremy@hinegardner.org