#--
# Adapted from rand.rb by:
# * Ilmari Heikkinen <mailto:kig@misfiring.net>
# * Christian Neukirchen <mailto:chneukirchen@gmail.com>
#++

require 'facets/core/hash/rand_key'

class Hash

  # Returns a random hash value.
  #
  #   {:one => 1, :two => 2, :three => 3}.rand_value  #=> 2
  #   {:one => 1, :two => 2, :three => 3}.rand_value  #=> 1
  #
  def rand_value
    fetch( rand_key )
  end

  # Deletes a random key-value pair and returns the value.
  #
  #   a = {:one => 1, :two => 2, :three => 3}
  #   a.at_rand!  #=> 2
  #   a           #=> {:one => 1, :three => 3}
  #
  def rand_value!
    k,v = rand_pair
    delete( k )
    return v
  end

  alias_method( :pick, :rand_value! )

  alias_method( :at_rand, :rand_value )
  alias_method( :at_rand!, :rand_value! )


end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCHash < Test::Unit::TestCase

    def test_rand_value
      h = { :a=>1, :b=>2, :c=>3 }
      10.times { assert( h.values.include?( h.rand_value ) ) }
    end

  end

=end
