-1

Select a element in the array randomly WITHOUT using the Random class.

id={1,2,3,4,5}  

Is there any method which belongs to the Array class that is used to randomly select an element?

walter
  • 89
  • 1
  • 11

1 Answers1

1

You can use the sample method

[:foo, :bar].sample # => :foo, or :bar :-)

Credit this SO question/answer

Community
  • 1
  • 1
MilesStanfield
  • 4,571
  • 1
  • 21
  • 32