### Home Classes Methods

### In Files

- lib/facets/random.rb

### Methods

- #at_rand

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Random
- Random::ArrayExtensions
- Random::HashExtensions
- Random::IntegerExtensions
- Random::NumericExtensions
- Random::RangeExtensions
- Random::StringExtensions
- Random::StringExtensions::Self
- FileUtils
- FileUtils::DryRun
- FileUtils::NoWrite
- FileUtils::Verbose
- Digest
- Digest::Class
- Digest::Instance
- Multiton
- Multiton::Inclusive
- Multiton::MetaMethods
- URI
- URI::Hash
- URI::Kernel
- Argvector
- Argvector
- ERB
- ERB::OpenTemplate
- Enumerable
- Enumerable::Arguments
- GetoptLong
- GetoptLong::DSL
- Memoizable
- Memoizable::Copy
- Numeric
- Numeric::Multipliers
- Object
- Object::ArgVector
- Timer
- Timer::Dummy
- Array
- BasicObject
- CGI
- Cloneable
- Continuation
- Date
- DateTime
- Equitable
- File
- Find
- Hash
- Instantiable
- Integer
- Interval
- Kernel
- Math
- Module
- Net
- NilClass
- NullClass
- OpEsc
- OpenStruct
- Pathname
- Platform
- Range
- RbConfig
- Set
- Shellwords
- String
- StringScanner
- Time
- Tuple
- YAML
- Zlib

No matching classes.

# Random::RangeExtensions

### Public Instance Methods

at_rand() click to toggle source

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

    # File lib/facets/random.rb, line 68
    def at_rand
      first, last = first(), last()
      if first.respond_to?(:random_delta)  # TODO: work on this!!!
        begin
          first.random_delta(last, exclude_end?)
        rescue
          to_a.at_rand
        end
      else
        to_a.at_rand
      end
      ##elsif first.respond_to?(:succ)
      ##  # optimized algorithm
      ##  if (Fixnum === first || Bignum === first) &&
      ##     (Fixnum === last  || Bignum === last)
      ##    last -= 1 if exclude_end?
      ##    return nil if last < first
      ##    return Random.number(last - first + 1) + first
      ##  end
      ##  # standard algorithm
      ##  return to_a.at_rand
      ##elsif Numeric === first && Numeric === last
      ##  return nil if last < first
      ##  return nil if exclude_end? && last == first
      ##  return (last - first) * Random.number + first
      ##else
      ##  return nil
      ##end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.