Project

General

Profile

Actions

Feature #12775

closed

Random subset of array

Added by sawa (Tsuyoshi Sawada) about 5 years ago. Updated 4 days ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:77330]

Description

I often see use cases to randomly sample a subset from an array like this:

a = [3, 2, 6, 2, 6, 1]
a.sample(rand(a.length + 1)) # => [2, 6, 6, 3]

I request extending Array#sample to let it take an option (such as :arbitrary) for doing that:

a.sample(:arbitrary) # => [2, 6, 6, 3]

or perhaps having an independent method (like samples) to do it.

a.samples # => [2, 6, 6, 3]
Actions

Also available in: Atom PDF