Module: Random::RangeExtensions

Included in:
Range
Defined in:
lib/standard/facets/random.rb

Overview

Random extensions for Range class.

Instance Method Summary collapse

Instance Method Details

#at_randObject

Return a random element from the range.

(1..4).at_rand           #~> 2
(1..4).at_rand           #~> 4

(1.5..2.5).at_rand       #~> 2.06309842754533
(1.5..2.5).at_rand       #~> 1.74976944931541

('a'..'z').at_rand       #~> 'q'
('a'..'z').at_rand       #~> 'f'

CREDIT: Lavir the Whiolet, Thomas Sawyer



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/standard/facets/random.rb', line 50

def at_rand
  first, last = first(), last()
  if first.respond_to?(:random_delta)
    begin
      first.random_delta(last, exclude_end?)
    rescue
      to_a.sample
    end
  else
    to_a.sample
  end
end