Velocity Reviews > Ruby > generating unique random numbers

# generating unique random numbers

Michael Fellinger
Guest
Posts: n/a

 03-25-2008
T24gVHVlLCBNYXIgMjUsIDIwMDggYXQgMTE6MjYgQU0sIFBlw7 FhLCBCb3RwIDxib3RwQGRlbG1v
bnRlLXBoaWwuY29tPiB3cm90ZToKPiBGcm9tOiBNaWNoYWVsIE ZlbGxpbmdlciBbbWFpbHRvOm0u
ZmVsbGluZ2VyQGdtYWlsLmNvbV0KPiAgIyAoMS4uMTMpLnNvcn RfYnl7IHJhbmQgfS5maXJzdCg3
KQo+Cj4gIGNhcmVmdWwsIE1pa2UgICAgICAgICAgICAgICBeXl 5eXl5eXgo+Cj4gIDspCgpvaCwg
aSBtZWFudCAuZmlyc3QoOCkgXl47CgpeIG1hbnZlcnUK

Subbu
Guest
Posts: n/a

 03-25-2008
On Mar 24, 6:05 pm, Tim Hunter <(E-Mail Removed)> 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. 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
Guest
Posts: n/a

 03-25-2008
On Tue, Mar 25, 2008 at 6:54 AM, Subbu <(E-Mail Removed)> wrote:
>
> On Mar 24, 6:05 pm, Tim Hunter <(E-Mail Removed)> 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
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?

http://realrand.rubyforge.org/

lasitha
Guest
Posts: n/a

 03-27-2008

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(

Array#shuffle is only available in ruby 1.9+ though.

Cheers, lasitha

--
View this message in context: http://www.nabble.com/generating-uni...p16322324.html
Sent from the ruby-talk mailing list archive at Nabble.com.