generating unique random numbers

Discussion in 'Ruby' started by Jimmy Palmer, Mar 25, 2008.

  1. Jimmy Palmer

    Jimmy Palmer Guest

    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, Mar 25, 2008
    #1
    1. Advertising

  2. Jimmy Palmer

    Tim Hunter Guest

    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
     
    Tim Hunter, Mar 25, 2008
    #2
    1. Advertising

  3. [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 <>
    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/.
    >
    >
     
    Shawn Anderson, Mar 25, 2008
    #3
  4. Jimmy Palmer

    Jimmy Palmer Guest

    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/.
     
    Jimmy Palmer, Mar 25, 2008
    #4
  5. Jimmy Palmer

    Chris Shea Guest

    On Mar 24, 7:00 pm, 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?
    > --
    > 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
     
    Chris Shea, Mar 25, 2008
    #5
  6. Jimmy Palmer

    Tim Hunter Guest

    Tim Hunter, Mar 25, 2008
    #6
  7. On Tue, Mar 25, 2008 at 10:26 AM, Tim Hunter <> 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 :)
     
    Michael Fellinger, Mar 25, 2008
    #7
  8. RnJvbTogTWljaGFlbCBGZWxsaW5nZXIgW21haWx0bzptLmZlbGxpbmdlckBnbWFpbC5jb21dIA0K
    IyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3KQ0KDQpjYXJlZnVsLCBNaWtlICAgICAg
    ICAgICAgICAgXl5eXl5eXl4NCg0KOykNCg==
     
    Peña, Botp, Mar 25, 2008
    #8
  9. Jimmy Palmer

    Todd Benson Guest

    On Mon, Mar 24, 2008 at 9:08 PM, Michael Fellinger
    <> wrote:
    > On Tue, Mar 25, 2008 at 10:26 AM, Tim Hunter <> 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
     
    Todd Benson, Mar 25, 2008
    #9
  10. On Tue, Mar 25, 2008 at 10:24:54AM +0900, Chris Shea wrote:
    > On Mar 24, 7:00 pm, 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?
    > > --
    > > 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, Mar 25, 2008
    #10
  11. T24gVHVlLCBNYXIgMjUsIDIwMDggYXQgMTE6MjYgQU0sIFBlw7FhLCBCb3RwIDxib3RwQGRlbG1v
    bnRlLXBoaWwuY29tPiB3cm90ZToKPiBGcm9tOiBNaWNoYWVsIEZlbGxpbmdlciBbbWFpbHRvOm0u
    ZmVsbGluZ2VyQGdtYWlsLmNvbV0KPiAgIyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3
    KQo+Cj4gIGNhcmVmdWwsIE1pa2UgICAgICAgICAgICAgICBeXl5eXl5eXgo+Cj4gIDspCgpvaCwg
    aSBtZWFudCAuZmlyc3QoOCkgXl47CgpeIG1hbnZlcnUK
     
    Michael Fellinger, Mar 25, 2008
    #11
  12. Jimmy Palmer

    Subbu Guest

    On Mar 24, 6:05 pm, Tim Hunter <> 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.
     
    Subbu, Mar 25, 2008
    #12
  13. Jimmy Palmer

    Robert Dober Guest

    On Tue, Mar 25, 2008 at 6:54 AM, Subbu <> wrote:
    >
    > On Mar 24, 6:05 pm, Tim Hunter <> 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
     
    Robert Dober, Mar 25, 2008
    #13
  14. 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/
     
    Reid Thompson, Mar 25, 2008
    #14
  15. Jimmy Palmer

    lasitha Guest

    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.
     
    lasitha, Mar 27, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wally
    Replies:
    1
    Views:
    2,824
    pvdg42
    Mar 20, 2006
  2. Replies:
    3
    Views:
    12,726
    Boris Stumm
    Feb 9, 2006
  3. lallous
    Replies:
    5
    Views:
    606
    lallous
    Oct 20, 2003
  4. Xoomer

    Unique Random Numbers

    Xoomer, Mar 25, 2007, in forum: C++
    Replies:
    17
    Views:
    557
    Alexander D. B. Kim
    Apr 11, 2007
  5. alanbe
    Replies:
    15
    Views:
    208
    Michael Winter
    Jun 3, 2005
Loading...

Share This Page