[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

generating unique random numbers

Jimmy Palmer

3/25/2008 1:01:00 AM

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-....

14 Answers

Tim Hunter

3/25/2008 1:06:00 AM

0

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.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Shawn Anderson

3/25/2008 1:09:00 AM

0

[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-....
>
>

Jimmy Palmer

3/25/2008 1:18:00 AM

0

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-....

Chris Shea

3/25/2008 1:23:00 AM

0

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-....

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

3/25/2008 1:26:00 AM

0

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.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Michael Fellinger

3/25/2008 2:09:00 AM

0

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

3/25/2008 2:26:00 AM

0

RnJvbTogTWljaGFlbCBGZWxsaW5nZXIgW21haWx0bzptLmZlbGxpbmdlckBnbWFpbC5jb21dIA0K
IyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3KQ0KDQpjYXJlZnVsLCBNaWtlICAgICAg
ICAgICAgICAgXl5eXl5eXl4NCg0KOykNCg==

Todd Benson

3/25/2008 2:37:00 AM

0

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

3/25/2008 4:53:00 AM

0

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-....
>
> 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/q...

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


Michael Fellinger

3/25/2008 5:28:00 AM

0

T24gVHVlLCBNYXIgMjUsIDIwMDggYXQgMTE6MjYgQU0sIFBlw7FhLCBCb3RwIDxib3RwQGRlbG1v
bnRlLXBoaWwuY29tPiB3cm90ZToKPiBGcm9tOiBNaWNoYWVsIEZlbGxpbmdlciBbbWFpbHRvOm0u
ZmVsbGluZ2VyQGdtYWlsLmNvbV0KPiAgIyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3
KQo+Cj4gIGNhcmVmdWwsIE1pa2UgICAgICAgICAgICAgICBeXl5eXl5eXgo+Cj4gIDspCgpvaCwg
aSBtZWFudCAuZmlyc3QoOCkgXl47CgpeIG1hbnZlcnUK