This repository has been archived by the owner on Nov 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_test_data.rb
executable file
·63 lines (51 loc) · 1.88 KB
/
generate_test_data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby
def generate min, median, max, number, seed=nil
raise "need min < median < max" unless min < median and median < max
srand(seed) if seed
info = File.new("numbers.info","w")
info.puts "min=#{min} median=#{median} max=#{max} number=#{number} seed=#{seed}"
info.close
num_less_than = number / 2
num_greater_than = number / 2
less_than_range = median - min
greater_than_range = max - median
# decide how many copies of median to emit
# make it the average of the expected frequencies less than and greater than
# need to ensure it's a multiple of three so it retains it's median status
expected_freq_values_less_than = num_less_than.to_f / less_than_range
expected_freq_values_greater_than = num_greater_than.to_f / greater_than_range
average_freq = ((expected_freq_values_less_than + expected_freq_values_greater_than )/2).to_i
total_eq = average_freq/3*3
total_eq = 1 if total_eq == 0
total = num_less_than + num_greater_than + total_eq
# we want to spread the medians evenly through the set though so
# need to keep a guard against emitting a median too often
min_num_between_medians = total / total_eq
last_median = 0
while(total > 0) do
case rand(3)
when 0
if num_less_than > 0
puts rand(less_than_range) + min
num_less_than -= 1
end
when 1
if num_greater_than > 0
puts rand(greater_than_range) + median + 1
num_greater_than -= 1
end
when 2
if total_eq > 0 and last_median > min_num_between_medians
puts median
total_eq -= 1
last_median = 0
end
end
total = num_less_than + num_greater_than + total_eq
last_median += 1
end
end
raise "generate_test_data.rb min median max number_elements (seed)" unless ARGV.length==5 || ARGV.length==4
ARGV.map! { |arg| arg.to_f.to_i } # to_f to allow 1e6 notation
min, median, max, number, seed = ARGV
generate min, median, max, number, seed