Class: ERV::DiscreteUniformDistribution
- Inherits:
-
Distribution
- Object
- Distribution
- ERV::DiscreteUniformDistribution
- Defined in:
- lib/erv/discrete_uniform_distribution.rb
Instance Attribute Summary collapse
-
#mean ⇒ Object
readonly
Returns the value of attribute mean.
-
#variance ⇒ Object
readonly
Returns the value of attribute variance.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ DiscreteUniformDistribution
constructor
A new instance of DiscreteUniformDistribution.
- #sample ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ DiscreteUniformDistribution
Returns a new instance of DiscreteUniformDistribution.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/erv/discrete_uniform_distribution.rb', line 10 def initialize(opts={}) super(opts) raise ArgumentError unless opts[:max_value] @max = opts[:max_value].to_i @min = opts[:min_value].try(:to_i) || 0 @mean = (@max + @min) / 2.0 # See https://en.wikipedia.org/wiki/Discrete_uniform_distribution @variance = ((@max - @min + 1) ** 2 - 1) / 12.0 end |
Instance Attribute Details
#mean ⇒ Object (readonly)
Returns the value of attribute mean.
8 9 10 |
# File 'lib/erv/discrete_uniform_distribution.rb', line 8 def mean @mean end |
#variance ⇒ Object (readonly)
Returns the value of attribute variance.
8 9 10 |
# File 'lib/erv/discrete_uniform_distribution.rb', line 8 def variance @variance end |
Instance Method Details
#sample ⇒ Object
21 22 23 |
# File 'lib/erv/discrete_uniform_distribution.rb', line 21 def sample @min + @rng.rand(@max - @min) end |