Module: MoreCoreExtensions::ArrayRandom
- Defined in:
- lib/more_core_extensions/core_ext/array/random.rb
Instance Method Summary collapse
-
#random_element ⇒ Object
Picks an element randomly.
-
#random_index ⇒ Object
Picks a valid index randomly.
Instance Method Details
#random_element ⇒ Object
Picks an element randomly
[1, 2, 3, 4, 2, 4].random_element # => random element in Array
19 20 21 |
# File 'lib/more_core_extensions/core_ext/array/random.rb', line 19 def random_element sample end |
#random_index ⇒ Object
Picks a valid index randomly
[1, 2, 3, 4, 2, 4].random_index # => random number between 0..5
7 8 9 10 11 12 13 |
# File 'lib/more_core_extensions/core_ext/array/random.rb', line 7 def random_index case self.size when 0; nil when 1; 0 else rand(0...self.size) end end |