2020-01-26 04:27:40 -05:00
|
|
|
class Array
|
|
|
|
# call-seq:
|
2020-09-02 15:02:34 -04:00
|
|
|
# array.shuffle!(random: Random) -> array
|
2020-01-26 04:27:40 -05:00
|
|
|
#
|
2020-09-02 15:02:34 -04:00
|
|
|
# Shuffles the elements of +self+ in place.
|
|
|
|
# a = [1, 2, 3] #=> [1, 2, 3]
|
2020-11-12 02:56:15 -05:00
|
|
|
# a.shuffle! #=> [2, 3, 1]
|
|
|
|
# a #=> [2, 3, 1]
|
2020-01-26 04:27:40 -05:00
|
|
|
#
|
2020-09-02 15:02:34 -04:00
|
|
|
# The optional +random+ argument will be used as the random number generator:
|
2020-01-26 04:27:40 -05:00
|
|
|
# a.shuffle!(random: Random.new(1)) #=> [1, 3, 2]
|
|
|
|
def shuffle!(random: Random)
|
2020-05-31 02:52:32 -04:00
|
|
|
Primitive.rb_ary_shuffle_bang(random)
|
2020-01-26 04:27:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2020-09-02 15:02:34 -04:00
|
|
|
# array.shuffle(random: Random) -> new_ary
|
2020-01-26 04:27:40 -05:00
|
|
|
#
|
|
|
|
# Returns a new array with elements of +self+ shuffled.
|
2020-09-02 15:02:34 -04:00
|
|
|
# a = [1, 2, 3] #=> [1, 2, 3]
|
2020-11-12 02:56:15 -05:00
|
|
|
# a.shuffle #=> [2, 3, 1]
|
|
|
|
# a #=> [1, 2, 3]
|
2020-01-26 04:27:40 -05:00
|
|
|
#
|
2020-09-02 15:02:34 -04:00
|
|
|
# The optional +random+ argument will be used as the random number generator:
|
2020-01-26 04:27:40 -05:00
|
|
|
# a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
|
|
|
|
def shuffle(random: Random)
|
2020-05-31 02:52:32 -04:00
|
|
|
Primitive.rb_ary_shuffle(random)
|
2020-01-26 04:27:40 -05:00
|
|
|
end
|
2020-01-26 04:34:18 -05:00
|
|
|
|
|
|
|
# call-seq:
|
2020-09-02 15:02:34 -04:00
|
|
|
# array.sample(random: Random) -> object
|
|
|
|
# array.sample(n, random: Random) -> new_ary
|
|
|
|
#
|
|
|
|
# Returns random elements from +self+.
|
|
|
|
#
|
|
|
|
# When no arguments are given, returns a random element from +self+:
|
|
|
|
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
|
|
# a.sample # => 3
|
|
|
|
# a.sample # => 8
|
|
|
|
# If +self+ is empty, returns +nil+.
|
|
|
|
#
|
2020-11-12 02:58:10 -05:00
|
|
|
# When argument +n+ is given, returns a new \Array containing +n+ random
|
|
|
|
# elements from +self+:
|
2020-09-02 15:02:34 -04:00
|
|
|
# a.sample(3) # => [8, 9, 2]
|
|
|
|
# a.sample(6) # => [9, 6, 10, 3, 1, 4]
|
|
|
|
# Returns no more than <tt>a.size</tt> elements
|
|
|
|
# (because no new duplicates are introduced):
|
|
|
|
# a.sample(a.size * 2) # => [6, 4, 1, 8, 5, 9, 10, 2, 3, 7]
|
|
|
|
# But +self+ may contain duplicates:
|
|
|
|
# a = [1, 1, 1, 2, 2, 3]
|
|
|
|
# a.sample(a.size * 2) # => [1, 1, 3, 2, 1, 2]
|
2021-09-22 01:08:05 -04:00
|
|
|
# The argument +n+ must be a non-negative numeric value.
|
|
|
|
# The order of the result array is unrelated to the order of +self+.
|
2020-09-02 15:02:34 -04:00
|
|
|
# Returns a new empty \Array if +self+ is empty.
|
|
|
|
#
|
|
|
|
# The optional +random+ argument will be used as the random number generator:
|
|
|
|
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
2020-01-26 04:34:18 -05:00
|
|
|
# a.sample(random: Random.new(1)) #=> 6
|
|
|
|
# a.sample(4, random: Random.new(1)) #=> [6, 10, 9, 2]
|
|
|
|
def sample(n = (ary = false), random: Random)
|
2021-11-12 12:15:09 -05:00
|
|
|
if Primitive.mandatory_only?
|
|
|
|
# Primitive.cexpr! %{ rb_ary_sample(self, rb_cRandom, Qfalse, Qfalse) }
|
|
|
|
Primitive.ary_sample0
|
|
|
|
else
|
|
|
|
# Primitive.cexpr! %{ rb_ary_sample(self, random, n, ary) }
|
|
|
|
Primitive.ary_sample(random, n, ary)
|
|
|
|
end
|
2020-01-26 04:34:18 -05:00
|
|
|
end
|
2020-01-26 04:27:40 -05:00
|
|
|
end
|