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
# File 'lib/ruby-metrics/statistics/uniform_sample.rb', line 4

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

Instance Method Details

#clearObject



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

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

#sizeObject



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

def size
  @values.size
end

#update(value) ⇒ Object



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

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

#valuesObject



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

def values
  @values.dup
end