Velocity Reviews > Ruby > generating unique random numbers

# generating unique random numbers

Jimmy Palmer
Guest
Posts: n/a

 03-25-2008
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 via http://www.ruby-forum.com/.

Tim Hunter
Guest
Posts: n/a

 03-25-2008
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

Shawn Anderson
Guest
Posts: n/a

 03-25-2008
[Note: parts of this message were removed to make it a legal post.]

ar = []
while ar.length < 8
ar << rand(12) + 1
ar.uniq!
end

/Shawn

On Mon, Mar 24, 2008 at 6:00 PM, Jimmy Palmer <(E-Mail Removed)>
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 via http://www.ruby-forum.com/.
>
>

Jimmy Palmer
Guest
Posts: n/a

 03-25-2008
Tim Hunter wrote:
>
> ~\$ 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>

thanks Tim. i like it.
--
Posted via http://www.ruby-forum.com/.

Chris Shea
Guest
Posts: n/a

 03-25-2008
On Mar 24, 7:00 pm, Jimmy Palmer <(E-Mail Removed)> 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.

Chris

Tim Hunter
Guest
Posts: n/a

 03-25-2008
Jimmy Palmer wrote:
> Tim Hunter wrote:
>> ~\$ 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>

>
> thanks Tim. i like it.

You're welcome. Turns out you don't need the to_a. One less method.

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

Michael Fellinger
Guest
Posts: n/a

 03-25-2008
On Tue, Mar 25, 2008 at 10:26 AM, Tim Hunter <(E-Mail Removed)> wrote:
> Jimmy Palmer wrote:
> > Tim Hunter wrote:
> >> ~\$ 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>

> >
> > thanks Tim. i like it.

>
> You're welcome. Turns out you don't need the to_a. One less method.

(1..13).sort_by{ rand }.first(7)

my attempt to improve upon it

Peña, Botp
Guest
Posts: n/a

 03-25-2008
RnJvbTogTWljaGFlbCBGZWxsaW5nZXIgW21haWx0bzptLmZlbG xpbmdlckBnbWFpbC5jb21dIA0K
IyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3KQ0KDQ pjYXJlZnVsLCBNaWtlICAgICAg
ICAgICAgICAgXl5eXl5eXl4NCg0KOykNCg==

Todd Benson
Guest
Posts: n/a

 03-25-2008
On Mon, Mar 24, 2008 at 9:08 PM, Michael Fellinger
<(E-Mail Removed)> wrote:
> On Tue, Mar 25, 2008 at 10:26 AM, Tim Hunter <(E-Mail Removed)> wrote:
> > Jimmy Palmer wrote:
> > > Tim Hunter wrote:
> > >> ~\$ 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>
> > >
> > > thanks Tim. i like it.

> >
> > You're welcome. Turns out you don't need the to_a. One less method.

>
> (1..13).sort_by{ rand }.first(7)
>
> my attempt to improve upon it

Tim's shuffle and cut technique seems fishy to me at first, but I like
it. Now I'll be up nights reading my old probability texts

Todd

Jeremy Hinegardner
Guest
Posts: n/a

 03-25-2008
On Tue, Mar 25, 2008 at 10:24:54AM +0900, Chris Shea wrote:
> On Mar 24, 7:00 pm, Jimmy Palmer <(E-Mail Removed)> 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 http://www.velocityreviews.com/forums/(E-Mail Removed)