Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#shuffle

array.shuffle(random: Random) → new_ary

Returns a new array with elements of self shuffled.

Example
a = [1, 2, 3] #=> [1, 2, 3]
a.shuffle     #=> [2, 3, 1]
a             #=> [1, 2, 3]

The optional random argument will be used as the random number generator:

Example
a.shuffle(random: Random.new(1))  #=> [1, 3, 2]
See also #shuffle! #shift #shelljoin #size also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1