Class: RandomWalk

Inherits:
Object
  • Object
show all
Defined in:
lib/random-walk.rb

Class Method Summary collapse

Class Method Details

.generate(limits, array_length, step = 1) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/random-walk.rb', line 2

def self.generate(limits, array_length, step = 1)
  starting_point = Random.rand(limits)
  output_array = [starting_point]

  (array_length - 1).times do
    add_or_subtract = Random.rand(-step..step)
    end_point = output_array.last + add_or_subtract
    if end_point <= limits.max && end_point >= limits.min
      output_array << end_point
    else
      output_array << output_array.last
    end
  end

  return output_array
end