Project

General

Profile

Actions

Feature #12775

closed

Random subset of array

Added by sawa (Tsuyoshi Sawada) over 7 years ago. Updated over 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:77320]

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

Like0
Like0Like0Like0Like0Like0Like0