Class: Statistical::Rng::UniformDiscrete

Inherits:
Object
  • Object
show all
Defined in:
lib/statistical/rng/uniform_discrete.rb

Overview

Companion RNG class for the continuous uniform distribution. Requires a

distrbution object of the corresponding distribution

Author:

  • Vaibhav Yenamandra

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dobj, seed = Random.new_seed) ⇒ UniformDiscrete

Companion RNG class for the continuous uniform distribution. Requires a

distrbution object of the corresponding distribution

Parameters:

Author:

  • Vaibhav Yenamandra



24
25
26
27
28
29
30
31
32
33
# File 'lib/statistical/rng/uniform_discrete.rb', line 24

def initialize(dobj, seed = Random.new_seed)
  unless dobj.nil? || dobj.is_a?(Statistical::Distribution::UniformDiscrete)
    raise TypeError,
          "Expected Distribution object or nil, found #{dobj.class}"
  end
  @generator = Random.new(seed)
  @lower = dobj.lower
  @upper = dobj.upper
  @sdist = dobj
end

Instance Attribute Details

#generatorNumeric (readonly)

The prng created with ‘seed` for usage as a source of randomness to base the current RNG on

Returns:

  • (Numeric)

    the current value of generator



14
15
16
# File 'lib/statistical/rng/uniform_discrete.rb', line 14

def generator
  @generator
end

#lowerNumeric (readonly)

The lower bound of the uniform distribution

Returns:

  • (Numeric)

    the current value of lower



14
15
16
# File 'lib/statistical/rng/uniform_discrete.rb', line 14

def lower
  @lower
end

#upperNumeric (readonly)

The upper bound of the uniform distribution

Returns:

  • (Numeric)

    the current value of upper



14
15
16
# File 'lib/statistical/rng/uniform_discrete.rb', line 14

def upper
  @upper
end

Instance Method Details

#membersObject

A call to expose the underlying distribution’s support set

Returns:

  • The elements over which the distribution exists



70
71
72
# File 'lib/statistical/rng/uniform_discrete.rb', line 70

def members
  return @sdist.support
end

#randObject

Return the next random number from the sequence using Ruby’s ‘rand`

Returns:

  • next random number in the sequence

Author:

  • Vaibhav Yenamandra



40
41
42
43
# File 'lib/statistical/rng/uniform_discrete.rb', line 40

def rand
  n = members.count
  return members[@generator.rand(n)]
end

#typeObject

Return the type of the source distribution

Returns:

  • source distribution’s type

Author:

  • Vaibhav Yenamandra



63
64
65
# File 'lib/statistical/rng/uniform_discrete.rb', line 63

def type
  @sdist.class
end