Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

generating unique random numbers

0 views
Skip to first unread message

Jimmy Palmer

unread,
Mar 24, 2008, 9:00:32 PM3/24/08
to
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

unread,
Mar 24, 2008, 9:05:41 PM3/24/08
to
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

unread,
Mar 24, 2008, 9:09:18 PM3/24/08
to
[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 <modern...@gmail.com>
wrote:

Jimmy Palmer

unread,
Mar 24, 2008, 9:18:27 PM3/24/08
to
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.

Chris Shea

unread,
Mar 24, 2008, 9:23:23 PM3/24/08
to

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

unread,
Mar 24, 2008, 9:26:18 PM3/24/08
to
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.

Michael Fellinger

unread,
Mar 24, 2008, 10:08:46 PM3/24/08
to
On Tue, Mar 25, 2008 at 10:26 AM, Tim Hunter <TimH...@nc.rr.com> 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

unread,
Mar 24, 2008, 10:26:10 PM3/24/08
to
From: Michael Fellinger [mailto:m.fel...@gmail.com]
# (1..13).sort_by{ rand }.first(7)

careful, Mike ^^^^^^^^

;)

Todd Benson

unread,
Mar 24, 2008, 10:37:06 PM3/24/08
to

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

unread,
Mar 25, 2008, 12:52:42 AM3/25/08
to

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 jer...@hinegardner.org


Michael Fellinger

unread,
Mar 25, 2008, 1:28:06 AM3/25/08
to

oh, i meant .first(8) ^^;

^ manveru

Subbu

unread,
Mar 25, 2008, 1:53:13 AM3/25/08
to

Hey Tim, I am a bit lost here :) sort_by accepts a block. But I see
you are just passing it a method. How does this work? Does Ruby
convert the return value of the method to a block and then sorts it?
Do you mind explaining it for me? Thanks so much.

Robert Dober

unread,
Mar 25, 2008, 4:10:26 AM3/25/08
to
On Tue, Mar 25, 2008 at 6:54 AM, Subbu <subramani...@gmail.com> wrote:
>
> On Mar 24, 6:05 pm, Tim Hunter <TimHun...@nc.rr.com> 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

Reid Thompson

unread,
Mar 25, 2008, 6:55:54 PM3/25/08
to
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?


http://realrand.rubyforge.org/

lasitha

unread,
Mar 27, 2008, 1:19:15 AM3/27/08
to

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.


0 new messages