Pick a random element from an array

Suppose I have an array and I want to pick one element at random.

What would be the simplest way to do this?

The obvious way would be array[random index]. But perhaps there is something like ruby’s array.sample? Or if not could such a method be created by using an extension?

16 Answers
16

Leave a Comment