Class: RailsDataExplorer::Statistics::RngCategory

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_data_explorer/statistics/rng_category.rb

Overview

Responsibilities:

* Provide random categorical data. Useful for testing and demo data.

Instance Method Summary collapse

Constructor Details

#initialize(categories, category_probabilities = nil, rng = lambda { Kernel.rand }) ⇒ RngCategory

Returns a new instance of RngCategory.

Parameters:

  • categories (Array<Object>)

    the pool of available categories.

  • category_probabilities (Array, optional) (defaults to: nil)

    probability of each category.

  • rng (Proc, optional) (defaults to: lambda { Kernel.rand })

    lambda to generate random numbers which will be mapped to categories.



15
16
17
18
19
20
# File 'lib/rails_data_explorer/statistics/rng_category.rb', line 15

def initialize(categories, category_probabilities = nil, rng = lambda { Kernel.rand })
  @categories, @category_probabilities, @rng = categories, category_probabilities, rng
  @category_probabilities ||= @categories.map { |e| @rng.call }
  @category_probabilities = normalize_category_probabilities
  @category_order = compute_category_order
end

Instance Method Details

#randObject

Returns a random category



23
24
25
26
27
28
29
# File 'lib/rails_data_explorer/statistics/rng_category.rb', line 23

def rand
  r_v = @rng.call
  rnd = @category_order.detect { |e|
    e[:threshold] >= r_v
  }
  rnd[:category]
end