Class: RailsDataExplorer::Statistics::RngCategory
- Inherits:
-
Object
- Object
- RailsDataExplorer::Statistics::RngCategory
- 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
-
#initialize(categories, category_probabilities = nil, rng = lambda { Kernel.rand }) ⇒ RngCategory
constructor
A new instance of RngCategory.
-
#rand ⇒ Object
Returns a random category.
Constructor Details
#initialize(categories, category_probabilities = nil, rng = lambda { Kernel.rand }) ⇒ RngCategory
Returns a new instance of RngCategory.
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
#rand ⇒ Object
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 |