Class: RubyOnAcid::RandomWalkFactory

Inherits:
Factory
  • Object
show all
Defined in:
lib/rubyonacid/factories/random_walk.rb

Overview

Increments from the minimum value, stopping at the maximum, or decrements from the maximum value, stopping at the minimum.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Factory

#boolean, #choose, #get, #within

Constructor Details

#initialize(interval = 0.001) ⇒ RandomWalkFactory

Returns a new instance of RandomWalkFactory.



11
12
13
14
15
16
# File 'lib/rubyonacid/factories/random_walk.rb', line 11

def initialize(interval = 0.001)
  super
  @start_value = 0.0
  @values = {}
  @interval = interval
end

Instance Attribute Details

#intervalObject

The maximum amount to change counters by.



9
10
11
# File 'lib/rubyonacid/factories/random_walk.rb', line 9

def interval
  @interval
end

Instance Method Details

#get_unit(key) ⇒ Object

Increment counter for given key and return it. Constrain between 0 and 1.



19
20
21
22
23
24
25
# File 'lib/rubyonacid/factories/random_walk.rb', line 19

def get_unit(key)
  @values[key] ||= rand
  @values[key] += (rand * (2 * @interval)) - @interval
  @values[key] = 1.0 if @values[key] > 1.0
  @values[key] = 0.0 if @values[key] < 0.0
  @values[key]
end