Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Array#shuffle!

array.shuffle!(random: Random) → array

Shuffles the elements of self in place.

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

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 #size #shift #slice also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0