Project

General

Profile

Actions

Misc #14147

closed

Array#sample documentation is unclear about order

Added by StefanPochmann (Stefan Pochmann) almost 4 years ago. Updated about 1 month ago.

Status:
Closed
Priority:
Normal
Assignee:
-
[ruby-core:84046]

Description

Does Array#sample only guarantee a random subset, or also random order? It's not clear. I think the current reference implementation does produce random order, but the documentation doesn't guarantee it, so I can't rely on it. If random order is intended, I think the documentation should say so. Maybe say that sample(n) is equivalent to shuffle.take(n)? ("equivalent" meaning same possible outputs with same probabilities)

More details here: https://stackoverflow.com/q/47600931/1672429

Updated by shevegen (Robert A. Heiler) almost 4 years ago

I agree; I think the documentation should mention that at http://ruby-doc.org/core/Array.html#method-i-sample. One can look at the source there :) but ... I guess more people will understand an explanation of the code, rather than the C code itself. :D

Actions #2

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Status changed from Open to Closed

Applied in changeset git|f8bf766877782468c4da8c1cecaf21ce9eac3684.


[DOC] Additional to Array#sample [ci skip]

  • The requirement to the n argument.
  • The order of the result array. [Misc #14147]
Actions

Also available in: Atom PDF