Class: Metrics::Statistics::UniformSample

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-metrics/statistics/uniform_sample.rb

Instance Method Summary collapse

Constructor Details

#initialize(size = 1028) ⇒ UniformSample

Returns a new instance of UniformSample.



4
5
6
7
8
9
# File 'lib/ruby-metrics/statistics/uniform_sample.rb', line 4

def initialize(size = 1028)
  @values = Array.new(size)
  @count = 0
  @size = size
  self.clear
end

Instance Method Details

#clearObject



11
12
13
14
15
16
# File 'lib/ruby-metrics/statistics/uniform_sample.rb', line 11

def clear
  (0..@values.length-1).each do |i|
    @values[i] = 0
  end
  @count = 0
end

#sizeObject



18
19
20
# File 'lib/ruby-metrics/statistics/uniform_sample.rb', line 18

def size
  @values.length
end

#update(value) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/ruby-metrics/statistics/uniform_sample.rb', line 22

def update(value)
  if @count < @values.length
    @values[@count] = value
    @count += 1
  else
    index = rand(@size) % @count
    @values[index] = value
  end
end

#valuesObject



32
33
34
# File 'lib/ruby-metrics/statistics/uniform_sample.rb', line 32

def values
  @values.dup
end