Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > generating unique random numbers

Reply
Thread Tools

generating unique random numbers

 
 
Michael Fellinger
Guest
Posts: n/a
 
      03-25-2008
T24gVHVlLCBNYXIgMjUsIDIwMDggYXQgMTE6MjYgQU0sIFBlw7 FhLCBCb3RwIDxib3RwQGRlbG1v
bnRlLXBoaWwuY29tPiB3cm90ZToKPiBGcm9tOiBNaWNoYWVsIE ZlbGxpbmdlciBbbWFpbHRvOm0u
ZmVsbGluZ2VyQGdtYWlsLmNvbV0KPiAgIyAoMS4uMTMpLnNvcn RfYnl7IHJhbmQgfS5maXJzdCg3
KQo+Cj4gIGNhcmVmdWwsIE1pa2UgICAgICAgICAgICAgICBeXl 5eXl5eXgo+Cj4gIDspCgpvaCwg
aSBtZWFudCAuZmlyc3QoOCkgXl47CgpeIG1hbnZlcnUK

 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
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/

 
Reply With Quote
 
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unique Random Numbers Xoomer C++ 17 04-11-2007 09:47 AM
Random number generator, generating 10 different numbers. Need Help. Wally ASP .Net 1 03-20-2006 12:19 AM
Generating Random Numbers between a potentially negative range laura.paterson@gmail.com Java 3 02-09-2006 09:04 AM
How to create an array of unique random numbers for an online quiz alanbe Javascript 15 06-03-2005 11:37 AM
Generating unique random numbers lallous C++ 5 10-20-2003 12:17 PM



Advertisments