Class: UniformSample

Inherits:
Object
  • Object
show all
Defined in:
lib/drone/utils/uniform_sample.rb

Instance Method Summary (collapse)

Constructor Details

- (UniformSample) initialize(size)



3
4
5
6
# File 'lib/drone/utils/uniform_sample.rb', line 3

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

Instance Method Details

- (Object) clear



8
9
10
11
12
13
14
# File 'lib/drone/utils/uniform_sample.rb', line 8

def clear
  @values.size.times do |n|
    @values[n] = 0
  end
  
  @count = 0
end

- (Object) size



16
17
18
# File 'lib/drone/utils/uniform_sample.rb', line 16

def size
  (@count > @values.size) ? @values.size : @count
end

- (Object) update(val)



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/drone/utils/uniform_sample.rb', line 20

def update(val)
  @count += 1
  if @count <= @values.size
    @values[@count - 1] = val
  else
    r = rand(2**64 - 1) % @count
    if r < @values.size
      @values[r] = val
    end
  end
end

- (Object) values



32
33
34
35
# File 'lib/drone/utils/uniform_sample.rb', line 32

def values
  # only return @count elements
  @values[0,@count]
end