Get a random value in a hash : rand « Development « Ruby






Get a random value in a hash


m = { :key1 => 'value1',
      :key2 => 'value2',
      :key3 => 'value3' }
values = m.values
values[rand(values.size)]            # => "value1"

 








Related examples in the same category

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