generating unique random numbers

J

Jimmy Palmer

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?
 
T

Tim Hunter

Jimmy said:
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>
 
S

Shawn Anderson

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

Chris Shea

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?

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
 
P

Peña, Botp

RnJvbTogTWljaGFlbCBGZWxsaW5nZXIgW21haWx0bzptLmZlbGxpbmdlckBnbWFpbC5jb21dIA0K
IyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3KQ0KDQpjYXJlZnVsLCBNaWtlICAgICAg
ICAgICAgICAgXl5eXl5eXl4NCg0KOykNCg==
 
T

Todd Benson

Jimmy said:
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
 
J

Jeremy Hinegardner

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?

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
 
M

Michael Fellinger

T24gVHVlLCBNYXIgMjUsIDIwMDggYXQgMTE6MjYgQU0sIFBlw7FhLCBCb3RwIDxib3RwQGRlbG1v
bnRlLXBoaWwuY29tPiB3cm90ZToKPiBGcm9tOiBNaWNoYWVsIEZlbGxpbmdlciBbbWFpbHRvOm0u
ZmVsbGluZ2VyQGdtYWlsLmNvbV0KPiAgIyAoMS4uMTMpLnNvcnRfYnl7IHJhbmQgfS5maXJzdCg3
KQo+Cj4gIGNhcmVmdWwsIE1pa2UgICAgICAgICAgICAgICBeXl5eXl5eXgo+Cj4gIDspCgpvaCwg
aSBtZWFudCAuZmlyc3QoOCkgXl47CgpeIG1hbnZlcnUK
 
S

Subbu

Jimmy said:
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>

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

Robert Dober

Jimmy said:
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>

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
 
R

Reid Thompson

Jimmy said:
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/
 
L

lasitha

Tim said:
Jimmy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top