Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_sample.rb

Overview

Implements methods for getting weighted random samples with and without replacement, as well as regular random samples

Instance Method Summary collapse

Instance Method Details

#sample(number = 1) ⇒ Hash Also known as: samples

Choose a random key=>value pair or n random pairs from the hash.

Each element doesn’t includes more than once.

If the hash is empty it returns an empty hash.

If the hash contains less than n unique keys, the copy of whole hash will be returned, none of keys will be lost.

Returns:

  • (Hash)

    new Hash containing sample key=>value pairs



18
19
20
# File 'lib/hash_sample.rb', line 18

def sample(number = 1)
  to_a.sample(number).to_h
end

#weighted_choiceObject #weighted_choices(n) ⇒ Array Also known as: wchoice

Choose 1 or n random keys from the hash, according to weights defined in hash values (weighted random sampling with replacement)

The keys are chosen by using random according to its weights and can be repeated in result. If the hash is empty the first form returns nil and the second form returns an empty array. All weights should be Numeric. Zero or negative weighs will be ignored.

Example
p {'_' => 9, 'a' => 1}.weighted_choices(10)  # ["_", "a", "_", "_", "_", "_", "_", "_", "_", "_"]

Overloads:

  • #weighted_choiceObject

    Returns one sample object.

    Returns:

    • (Object)

      one sample object

  • #weighted_choices(n) ⇒ Array

    Returns Array of n samples.

    Parameters:

    • n (Integer)

      number of samples to be returned

    Returns:

    • (Array)

      Array of n samples



43
44
45
# File 'lib/hash_sample.rb', line 43

def weighted_choice
  weighted_choices(1).first
end

#weighted_choices(number = 1) ⇒ Object Also known as: wchoices



47
48
49
50
51
52
# File 'lib/hash_sample.rb', line 47

def weighted_choices(number = 1)
  return [] if empty?

  validate_weights
  Array.new(number) { _wrs.first }
end

#weighted_sampleObject #weighted_samples(number) ⇒ Array Also known as: wsample

Choose 1 or number of distinct random keys from the hash, according to weights defined in hash values (weighted random sampling without replacement)

When there are no sufficient distinct samples to return, the result will contain less than specified number of samples

If the hash is empty the first form returns nil and the second form returns an empty array. All weights should be Numeric. Objects with zero or negative weighs will be skipped.

Example
{'a' => 98, 'b' => 1, 'c' => 1}.weighted_sample      # 'a'
{'a' => 98, 'b' => 1, 'c' => 1}.weighted_samples(3)  # ['a', 'a', 'a']
{'_' => 9, 'a' => 1}.weighted_samples(10)            # ['_', 'a']

Overloads:

  • #weighted_sampleObject

    Returns one sample object.

    Returns:

    • (Object)

      one sample object

  • #weighted_samples(number) ⇒ Array

    Returns Array of specified or sometimes less than specified samples.

    Parameters:

    • number (Integer)

      number of samples to be returned

    Returns:

    • (Array)

      Array of specified or sometimes less than specified samples



79
80
81
# File 'lib/hash_sample.rb', line 79

def weighted_sample
  weighted_samples(1).first
end

#weighted_samples(number = 1) ⇒ Object Also known as: wsamples



83
84
85
86
87
88
# File 'lib/hash_sample.rb', line 83

def weighted_samples(number = 1)
  return [] if empty?

  validate_weights
  _wrs(number).map(&:first)
end