vaibhav-y/statistical

View on GitHub
data/template/rng.erb

Summary

Maintainability
Test Coverage
require 'statistical/distribution/<%= distribution%>'

module Statistical
  # Companion RNG class for the continuous <%= distribution.capitalize.camelcase %> distribution. Requires a
  #   distrbution object of the corresponding distribution
  #
  # @author Vaibhav Yenamandra
  #
  # @attr_reader [Numeric] lower The lower bound of the <%= distribution.capitalize.camelcase %> distribution.
  # @attr_reader [Numeric] upper The upper bound of the <%= distribution.capitalize.camelcase %> distribution.
  module Rng
    class <%= distribution.capitalize.camelcase %>
      attr_reader :generator, # other params go here
      
      def initialize(dobj = nil, seed = Random.new_seed)
        unless dobj.nil? || dobj.is_a?(Statistical::Distribution::<%= distribution.capitalize.camelcase %>)
          raise TypeError, "Expected Distribution object or nil, found #{dobj.class}"
        end
        dobj = Statistical::Distribution::<%= distribution.capitalize.camelcase %>.new if dobj.nil?
        @generator = Random.new(seed)
        # Map other parameters here
        @sdist = dobj
      end

      # Return the next random number from the sequence
      #
      # @return [Float] next random number in the sequence
      def rand
        @sdist.quantile(@generator.rand)
      end

      # Compare against another rng to see if they are the same
      #
      # @return true if and only if, source distributions are the same and the
      #   prng has the same initial state
      def eql?(other)
        return other.is_a?(self.class) &&
               @generator == other.generator
      end

      # Return the type of the source distribution
      #
      # @return source distribution's type
      def type
        @sdist.class
      end

      alias_method :==, :eql?
      private :eql?
    end
  end
end