Array#sample is Set#sample and not Array#sample!

Arrays are ordered, sets are not. Sampling an array should give random
elements in the good order. But :

irb(main):001:0> (0…10).to_a.sample(5)
=> [8, 10, 4, 6, 7]

Today’s Array#sample actually is Set#sample, but Set#sample does not
exist.

irb(main):002:0> Set.new((0…10)).sample(5)
NoMethodError: undefined method `sample’ for #<Set: {0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10}>

_md

On Mon, Jun 25, 2012 at 4:53 PM, Michel D. [email protected]
wrote:

Arrays are ordered, sets are not. Sampling an array should give random
elements in the good order. But :

irb(main):001:0> (0…10).to_a.sample(5)
=> [8, 10, 4, 6, 7]

Where is the problem with that? What do you mean by “good order”?

Today’s Array#sample actually is Set#sample, but Set#sample does not
exist.

irb(main):002:0> Set.new((0…10)).sample(5)
NoMethodError: undefined method `sample’ for #<Set: {0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10}>

I am sorry, I don’t get it. Can you explain what your expectation is?

Kind regards

robert

2012/6/25 Robert K. [email protected]:

I am sorry, I don’t get it. Can you explain what your expectation is?

I think he wants to keep the ordering or original array in the result
of #sample.

This might actually make sense.

– Matma R.

Am 25.06.2012 18:55, schrieb Bartosz Dziewoński:

2012/6/25 Robert K. [email protected]:

I am sorry, I don’t get it. Can you explain what your expectation is?

I think he wants to keep the ordering or original array in the result
of #sample.

This might actually make sense.

But Set#sample could not provide an unordered random sample from a
collection of values with duplicates.

Marcus

On Monday, June 25, 2012 10:53:51 AM UTC-4, Michel D. wrote:

irb(main):002:0> Set.new((0…10)).sample(5)
NoMethodError: undefined method `sample’ for #<Set: {0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10}>

I think that would require a different method, say #ordered_sample.

If you write it and I’ll add it to Facets random.rb.

On 06/25/2012 11:03 PM, Ryan D. wrote:

So you would expect (1..3).to_a.sample(3) to always output [1, 2, 3] ?

That doesn’t make sense to me.

I agree, sample isn’t intended to work the way the OP suggests.

There are other ways to achieve the desired effect:

class Array
def ordered_sample(n)
values_at [*0…size].sample(n).sort
end
end

a = [:a, :b, :c, :d, :e, :f, :g, :h]

a.ordered_sample(4)
=> [:b, :c, :d, :e]
a.ordered_sample(4)
=> [:c, :d, :e, :h]
a.ordered_sample(4)
=> [:a, :b, :c, :g]

On Jun 25, 2012, at 09:55 , Bartosz Dziewoński wrote:

2012/6/25 Robert K. [email protected]:

I am sorry, I don’t get it. Can you explain what your expectation is?

I think he wants to keep the ordering or original array in the result
of #sample.

This might actually make sense.

So you would expect (1..3).to_a.sample(3) to always output [1, 2, 3] ?

That doesn’t make sense to me.

(1…3).to_a.sample(3)
=> [1, 3, 2]
(1…3).to_a.sample(3)
=> [1, 2, 3]
(1…3).to_a.sample(3)
=> [2, 1, 3]

On 06/26/2012 04:50 PM, Josh C. wrote:

    end

Maybe we’re on different rubies, but I had to change the implementation
to values_at *[*0...size].sample(n).sort

You’re right, I did a copy&paste error and forgot to test my latest edit
to the email.

A bit more readable:

values_at size.times.to_a.sample(n).sort

On Mon, Jun 25, 2012 at 9:53 AM, Michel D.
[email protected]wrote:

NoMethodError: undefined method `sample’ for #<Set: {0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10}>

Sample selects a random element from the array, so it makes sense that
sample(5) takes 5 random elements from the array.

It sounds like you’re looking for take:
[*0…10].take(5) # => [0, 1, 2, 3, 4]

Or maybe take with random intervals?

On Tue, Jun 26, 2012 at 4:48 AM, Lars H.
[email protected]
wrote:

a = [:a, :b, :c, :d, :e, :f, :g, :h]

a.ordered_sample(4)
=> [:b, :c, :d, :e]
a.ordered_sample(4)
=> [:c, :d, :e, :h]
a.ordered_sample(4)
=> [:a, :b, :c, :g]

Maybe we’re on different rubies, but I had to change the implementation
to
values_at *[*0...size].sample(n).sort

Bartosz Dziewoński wrote in post #1065970:

2012/6/25 Robert K. [email protected]:

I am sorry, I don’t get it. Can you explain what your expectation is?

I think he wants to keep the ordering or original array in the result
of #sample.

This might actually make sense.

– Matma R.

I just mean that array.sample(n) does not depend on the Array ‘array’
but of the underlining set. It should be a method of Set.

For the rest, it is only a question of definition and taste.
_md

Am 26.06.2012 18:23, schrieb Michel D.:

– Matma R.

I just mean that array.sample(n) does not depend on the Array ‘array’
but of the underlining set. It should be a method of Set.

For the rest, it is only a question of definition and taste.
_md

irb(main):001:0> [1, 1, 1, 1, 2, 3, 4].sample(4)
=> [1, 1, 1, 2]
irb(main):002:0> [1, 1, 1, 1, 2, 3, 4].sample(4)
=> [4, 1, 1, 2]

How would you do that with a set?

Regards,
Marcus