# generating unique random numbers

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

1. ### Jimmy PalmerGuest

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

2. ### Tim HunterGuest

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

3. ### Shawn AndersonGuest

[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
4. ### Jimmy PalmerGuest

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
5. ### Chris SheaGuest

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
6. ### Tim HunterGuest

Tim Hunter, Mar 25, 2008
7. ### Michael FellingerGuest

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
8. ### PeÃ±a, BotpGuest

RnJvbTogTWljaGFlbCBGZWxsaW5nZXIgW21haWx0bzptLmZlbGxpbmdlckBnbWFpbC5jb21dIA0K
IyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3KQ0KDQpjYXJlZnVsLCBNaWtlICAgICAg
ICAgICAgICAgXl5eXl5eXl4NCg0KOykNCg==

PeÃ±a, Botp, Mar 25, 2008
9. ### Todd BensonGuest

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
10. ### Jeremy HinegardnerGuest

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
11. ### Michael FellingerGuest

T24gVHVlLCBNYXIgMjUsIDIwMDggYXQgMTE6MjYgQU0sIFBlw7FhLCBCb3RwIDxib3RwQGRlbG1v
bnRlLXBoaWwuY29tPiB3cm90ZToKPiBGcm9tOiBNaWNoYWVsIEZlbGxpbmdlciBbbWFpbHRvOm0u
ZmVsbGluZ2VyQGdtYWlsLmNvbV0KPiAgIyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3
KQo+Cj4gIGNhcmVmdWwsIE1pa2UgICAgICAgICAgICAgICBeXl5eXl5eXgo+Cj4gIDspCgpvaCwg
aSBtZWFudCAuZmlyc3QoOCkgXl47CgpeIG1hbnZlcnUK

Michael Fellinger, Mar 25, 2008
12. ### SubbuGuest

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
13. ### Robert DoberGuest

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
14. ### Reid ThompsonGuest

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
15. ### lasithaGuest

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