method
sample
v1_9_1_378 -
Show latest stable
- Class:
Array
sample(p1)public
Choose a random element, or the random n elements, from the array. If the array is empty, the first form returns nil, and the second form returns an empty array.
static VALUE
rb_ary_sample(int argc, VALUE *argv, VALUE ary)
{
VALUE nv, result, *ptr;
long n, len, i, j, k, idx[10];
len = RARRAY_LEN(ary);
if (argc == 0) {
if (len == 0) return Qnil;
i = len == 1 ? 0 : rb_genrand_real()*len;
return RARRAY_PTR(ary)[i];
}
rb_scan_args(argc, argv, "1", &nv);
n = NUM2LONG(nv);
if (n < 0) rb_raise(rb_eArgError, "negative sample number");
ptr = RARRAY_PTR(ary);
len = RARRAY_LEN(ary);
if (n > len) n = len;
switch (n) {
case 0: return rb_ary_new2(0);
case 1:
return rb_ary_new4(1, &ptr[(long)(rb_genrand_real()*len)]);
case 2:
i = rb_genrand_real()*len;
j = rb_genrand_real()*(len-1);
if (j >= i) j++;
return rb_ary_new3(2, ptr[i], ptr[j]);
case 3:
i = rb_genrand_real()*len;
j = rb_genrand_real()*(len-1);
k = rb_genrand_real()*(len-2);
{
long l = j, g = i;
if (j >= i) l = i, g = ++j;
if (k >= l && (++k >= g)) ++k;
}
return rb_ary_new3(3, ptr[i], ptr[j], ptr[k]);
}
if (n < sizeof(idx)/sizeof(idx[0])) {
long sorted[sizeof(idx)/sizeof(idx[0])];
sorted[0] = idx[0] = rb_genrand_real()*len;
for (i=1; i<n; i++) {
k = rb_genrand_real()*--len;
for (j = 0; j < i; ++j) {
if (k < sorted[j]) break;
++k;
}
memmove(&sorted[j+1], &sorted[j], sizeof(sorted[0])*(i-j));
sorted[j] = idx[i] = k;
}
result = rb_ary_new2(n);
for (i=0; i<n; i++) {
RARRAY_PTR(result)[i] = RARRAY_PTR(ary)[idx[i]];
}
}
else {
result = rb_ary_new4(len, ptr);
RB_GC_GUARD(ary);
for (i=0; i<n; i++) {
j = (long)(rb_genrand_real()*(len-i)) + i;
nv = RARRAY_PTR(result)[j];
RARRAY_PTR(result)[j] = RARRAY_PTR(result)[i];
RARRAY_PTR(result)[i] = nv;
}
}
ARY_SET_LEN(result, n);
return result;
} Related methods
- Instance methods
- &
- *
- +
- -
- <<
- <=>
- ==
- []
- []=
- abbrev
- assoc
- at
- clear
- collect
- collect!
- combination
- compact
- compact!
- concat
- count
- cycle
- dclone
- delete
- delete_at
- delete_if
- drop
- drop_while
- each
- each_index
- empty?
- eql?
- fetch
- fill
- find_index
- first
- flatten
- flatten!
- frozen?
- hash
- include?
- index
- initialize_copy
- insert
- inspect
- join
- last
- length
- map
- map!
- pack
- permutation
- pop
- pretty_print
- pretty_print_cycle
- product
- push
- rassoc
- reject
- reject!
- replace
- reverse
- reverse!
- reverse_each
- rindex
- sample
- select
- shelljoin
- shift
- shuffle
- shuffle!
- size
- slice
- slice!
- sort
- sort!
- take
- take_while
- to_a
- to_ary
- to_csv
- to_s
- to_yaml
- transpose
- uniq
- uniq!
- unshift
- values_at
- yaml_initialize
- zip
- |
- Class methods
- []
- new
- try_convert