Project

General

Profile

Actions

Feature #3647

closed

Array#sample(n, replace=false)

Added by mrkn (Kenta Murata) over 14 years ago. Updated over 4 years ago.

Status:
Feedback
Target version:
-
[ruby-dev:41918]

Description

Array#sample does not support sampling with replacement directly, so we must write codes like the following to obtain samples with replacement.

samples_with_replacement = n.times.map { source.sample }

If Array#sample has the argument for switching with/without replacement, we can write it as follow.

samples_with_replacement = source.sample(n, true)

The patch was attached. Please consider to merge.


Files

array_sample_with_replace.patch (3.64 KB) array_sample_with_replace.patch mrkn (Kenta Murata), 08/03/2010 01:13 PM
array_sample_with_replace_hash.patch (4.35 KB) array_sample_with_replace_hash.patch mrkn (Kenta Murata), 08/03/2010 04:53 PM
array_sample_shuffle.patch (9.62 KB) array_sample_shuffle.patch mrkn (Kenta Murata), 08/03/2010 06:25 PM

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #3649: Array#sample, Array#shuffle, Array#shuffle! で使用する乱数生成器を指定するためにオプショナル引数を追加するClosed08/03/2010Actions
Related to Ruby master - Feature #4247: New features for Array#sample, Array#choiceRejectedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0