Skip to content
instance method

Array#shuffle!

(?random: ::_Rand rng) -> self
shuffle!(random: Random) → self

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.

See also #shuffle #size #shift #slice also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0