Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   generating unique random numbers (http://www.velocityreviews.com/forums/t849162-generating-unique-random-numbers.html)

 Jimmy Palmer 03-25-2008 01:00 AM

generating unique random numbers

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 03-25-2008 01:05 AM

Re: generating unique random numbers

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 03-25-2008 01:09 AM

Re: generating unique random numbers

[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 <modernfossil@gmail.com>
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 03-25-2008 01:18 AM

Re: generating unique random numbers

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 03-25-2008 01:23 AM

Re: generating unique random numbers

On Mar 24, 7:00 pm, Jimmy Palmer <modernfos...@gmail.com> 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 03-25-2008 01:26 AM

Re: generating unique random numbers

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 03-25-2008 02:08 AM

Re: generating unique random numbers

On Tue, Mar 25, 2008 at 10:26 AM, Tim Hunter <TimHunter@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 03-25-2008 02:26 AM

Re: generating unique random numbers

RnJvbTogTWljaGFlbCBGZWxsaW5nZXIgW21haWx0bzptLmZlbG xpbmdlckBnbWFpbC5jb21dIA0K
IyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3KQ0KDQ pjYXJlZnVsLCBNaWtlICAgICAg
ICAgICAgICAgXl5eXl5eXl4NCg0KOykNCg==

 Todd Benson 03-25-2008 02:37 AM

Re: generating unique random numbers

On Mon, Mar 24, 2008 at 9:08 PM, Michael Fellinger
<m.fellinger@gmail.com> wrote:
> On Tue, Mar 25, 2008 at 10:26 AM, Tim Hunter <TimHunter@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 :)

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 03-25-2008 04:52 AM

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 <modernfos...@gmail.com> 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

All times are GMT. The time now is 07:14 AM.