random-walk 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +16 -9
- data/VERSION +1 -1
- data/lib/random-walk.rb +20 -2
- data/random-walk.gemspec +2 -2
- data/spec/random-walk_spec.rb +53 -8
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -2,18 +2,25 @@
|
|
2
2
|
|
3
3
|
This is an extremely simple gem for generating random-walk test data in an array. It's useful for statistics testing, where random data falls short for testing something like simple regression.
|
4
4
|
|
5
|
-
The resulting array will always stay within the
|
5
|
+
The resulting array will always stay within the range provided (inclusive). The random-walk adds +1, 0, or -1 on each iteration.
|
6
6
|
|
7
|
-
|
7
|
+
== Usage
|
8
8
|
|
9
|
-
|
10
|
-
require 'random-walk'
|
9
|
+
require 'random-walk'
|
11
10
|
|
12
|
-
# RandomWalk.generate(
|
13
|
-
RandomWalk.generate(10..40, 5) # [11, 12, 11, 10, 10]
|
14
|
-
RandomWalk.generate(10..40, 5) # [27, 28, 29, 30, 29]
|
15
|
-
|
16
|
-
|
11
|
+
# RandomWalk.generate(range, array_length)
|
12
|
+
RandomWalk.generate(10..40, 5) # [11, 12, 11, 10, 10]
|
13
|
+
RandomWalk.generate(10..40, 5) # [27, 28, 29, 30, 29]
|
14
|
+
|
15
|
+
# generate_vectors(x_range, y_range, vector_length)
|
16
|
+
rw = RandomWalk.new
|
17
|
+
rw.generate_vectors(0..99, 0..1023, 5)
|
18
|
+
rw.x # [78, 79, 80, 81, 81]
|
19
|
+
rw.y # [700, 701, 702, 702, 701]
|
20
|
+
|
21
|
+
# fit_y_to_x(fit_chance)
|
22
|
+
rw.fit_y_to_x(100) # Perfect fit. If x goes up by 1, y goes up by 1
|
23
|
+
rw.fit_y_to_x(50) # 50% chance. If x goes up by 1, y has a 50 percent chance of going up by 1
|
17
24
|
|
18
25
|
== Contributing to random-walk
|
19
26
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/random-walk.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
class RandomWalk
|
2
|
+
attr_accessor :x, :y
|
3
|
+
|
2
4
|
def self.generate(limits, array_length)
|
3
5
|
starting_point = Random.rand(limits)
|
4
6
|
output_array = [starting_point]
|
5
|
-
|
6
7
|
(array_length - 1).times do
|
7
8
|
add_or_subtract = Random.rand(-1..1)
|
8
9
|
end_point = output_array.last + add_or_subtract
|
@@ -12,7 +13,24 @@ class RandomWalk
|
|
12
13
|
output_array << output_array.last
|
13
14
|
end
|
14
15
|
end
|
15
|
-
|
16
16
|
return output_array
|
17
17
|
end
|
18
|
+
|
19
|
+
def generate_vectors(x_limits, y_limits, vector_length)
|
20
|
+
self.x = RandomWalk.generate(x_limits, vector_length)
|
21
|
+
self.y = RandomWalk.generate(y_limits, vector_length)
|
22
|
+
end
|
23
|
+
|
24
|
+
def fit_y_to_x(fit_chance)
|
25
|
+
raise 'Fit chance must be between 0 and 100' unless fit_chance <= 100 && fit_chance >= 0
|
26
|
+
self.x.each_with_index do |value, i|
|
27
|
+
if self.x[i] && self.x[i+1]
|
28
|
+
x_diff = self.x[i] - self.x[i+1]
|
29
|
+
rand = Random.rand(100 / fit_chance)
|
30
|
+
if rand == 0
|
31
|
+
self.y[i+1] = self.y[i] - x_diff
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
18
36
|
end
|
data/random-walk.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "random-walk"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Evan Tann"]
|
12
|
-
s.date = "2013-01-
|
12
|
+
s.date = "2013-01-07"
|
13
13
|
s.description = "Generates an array of random-walk data within user-specified limits and of an arbitrary length for use in testing."
|
14
14
|
s.email = "egtann@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/random-walk_spec.rb
CHANGED
@@ -1,17 +1,62 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "RandomWalk" do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
describe "generate" do
|
5
|
+
it "returns results between given limits" do
|
6
|
+
5.times do
|
7
|
+
results = RandomWalk.generate(0..10, 1000)
|
8
|
+
results.each do |result|
|
9
|
+
result.should be >= 0
|
10
|
+
result.should be <= 10
|
11
|
+
end
|
9
12
|
end
|
10
13
|
end
|
14
|
+
|
15
|
+
it "returns an array of the correct length" do
|
16
|
+
results = RandomWalk.generate(100..200, 250)
|
17
|
+
results.length.should == 250
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "generate_vectors" do
|
22
|
+
let(:rw) { RandomWalk.new }
|
23
|
+
|
24
|
+
it "returns results between given limits" do
|
25
|
+
5.times do
|
26
|
+
rw.generate_vectors(0..10, 0..10, 1000)
|
27
|
+
rw.x.each do |result|
|
28
|
+
result.should be >= 0
|
29
|
+
result.should be <= 10
|
30
|
+
end
|
31
|
+
rw.y.each do |result|
|
32
|
+
result.should be >= 0
|
33
|
+
result.should be <= 10
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns 2 arrays of the correct length" do
|
39
|
+
rw.generate_vectors(0..99, 0..1023, 500)
|
40
|
+
rw.x.length.should == 500
|
41
|
+
rw.y.length.should == 500
|
42
|
+
end
|
11
43
|
end
|
12
44
|
|
13
|
-
|
14
|
-
|
15
|
-
|
45
|
+
describe "fit_y_to_x" do
|
46
|
+
let(:rw) { RandomWalk.new }
|
47
|
+
|
48
|
+
it "fits the y array to the x array perfectly" do
|
49
|
+
rw.generate_vectors(0..99, 250..1000, 500)
|
50
|
+
fit_chance = 100
|
51
|
+
rw.fit_y_to_x(fit_chance)
|
52
|
+
diff_x, diff_y = 0, 0
|
53
|
+
rw.x.each_with_index do |result, i|
|
54
|
+
if rw.x[i+1]
|
55
|
+
diff_x += rw.x[i] - rw.x[i+1]
|
56
|
+
diff_y += rw.y[i] - rw.y[i+1]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
diff_x.should == diff_y
|
60
|
+
end
|
16
61
|
end
|
17
62
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random-walk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
111
|
version: '0'
|
112
112
|
segments:
|
113
113
|
- 0
|
114
|
-
hash: -
|
114
|
+
hash: -3032803507122637849
|
115
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|