Module: Capricious::PRNG
- Included in:
- Erlang, Exponential, Normal, Poisson, Uniform
- Defined in:
- lib/capricious/generic_prng.rb
Overview
Base mixin for distribution simulators. Manages an underlying PRNG and optional statistics. Mixing =PRNG= in to a simulator class will define
next= and =reset= methods as well as =@aggregate= and =@seed= attributes
and a =@prng= instance variable. Simulator classes must define a
next_value= method that returns a value in the given distribution and
should define =expected_mean= and =expected_variance= methods or attributes.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(seed = nil, policy = MWC5, keep_stats = false) ⇒ Object
Takes a seed, a policy, and whether or not to keep statistics in the =aggregate= attribute of the distribution object.
-
#next ⇒ Object
Returns the next value from this simulator.
- #prng_initialize(seed = nil, policy = MWC5, keep_stats = false) ⇒ Object
-
#reset(seed = nil) ⇒ Object
Resets the state of the underlying value.
Class Method Details
.included(base) ⇒ Object
60 61 62 63 64 |
# File 'lib/capricious/generic_prng.rb', line 60 def self.included(base) base.class_eval do attr_reader :aggregate, :seed end end |
Instance Method Details
#initialize(seed = nil, policy = MWC5, keep_stats = false) ⇒ Object
Takes a seed, a policy, and whether or not to keep statistics in the
aggregate= attribute of the distribution object. If a simulator class
overrides =initialize=, it must call =prng_initialize= from within its
initialize= method.
37 38 39 |
# File 'lib/capricious/generic_prng.rb', line 37 def initialize(seed=nil, policy=MWC5, keep_stats=false) prng_initialize(seed, policy, keep_stats) end |
#next ⇒ Object
Returns the next value from this simulator.
48 49 50 51 52 |
# File 'lib/capricious/generic_prng.rb', line 48 def next val = next_value @aggregate << val if @aggregate val end |
#prng_initialize(seed = nil, policy = MWC5, keep_stats = false) ⇒ Object
41 42 43 44 45 |
# File 'lib/capricious/generic_prng.rb', line 41 def prng_initialize(seed=nil, policy=MWC5, keep_stats=false) @prng = policy.new_with_seed(seed) @seed = @prng.seed @aggregate = SampleSink.new if keep_stats end |
#reset(seed = nil) ⇒ Object
Resets the state of the underlying value.
55 56 57 58 |
# File 'lib/capricious/generic_prng.rb', line 55 def reset(seed=nil) @prng.reset(seed) @aggregate = SampleSink.new if @aggregate end |