instance method
Array#shuffle!
(?random: ::_Rand rng) -> self
shuffle!(random: Random) → self
array.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/array.rb, line 22
def shuffle!(random: Random)
Primitive.rb_ary_shuffle_bang(random)
end
Shuffles all elements in self into a random order, as selected by the object given by the keyword argument random. Returns self:
Example
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a.shuffle! # => [5, 3, 8, 7, 6, 1, 9, 4, 2, 0] a.shuffle! # => [9, 4, 0, 6, 2, 8, 1, 5, 3, 7]
Duplicate elements are included:
Example
a = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] a.shuffle! # => [1, 0, 0, 1, 1, 0, 1, 0, 0, 1] a.shuffle! # => [0, 1, 0, 1, 1, 0, 1, 0, 1, 0]
The object given with the keyword argument random is used as the random number generator.
Related: see Methods for Assigning.