Get a random item in an array : rand « Development « Ruby






Get a random item in an array


a = ['item1', 'item2', 'item3']
a[rand(a.size)]                     # => "item3"

 








Related examples in the same category

1.Generating Random Numbers
2.Random with a range
3.Get a random value in a hash
4.Generate random numbers
5.Some random numbers based on process number and current time
6.Start the seed with the number 1