Skip to content

Instantly share code, notes, and snippets.

@ntijoh-daniel-berg
Created January 10, 2017 09:31
Show Gist options
  • Save ntijoh-daniel-berg/c7caaf5fc9e44084ad7d4973639d6f6e to your computer and use it in GitHub Desktop.
Save ntijoh-daniel-berg/c7caaf5fc9e44084ad7d4973639d6f6e to your computer and use it in GitHub Desktop.
class ArrayRandomizationComparison
def initialize(array:, method:, runs: 1000)
@array = array.dup
@method = method
@runs = runs
@comparison_matrix = Array.new(@array.size) {|_| Array.new(@array.size, 0)}
run
display
end
def run
@runs.times do
shuffled_array = @array.dup.send(@method)
shuffled_array.each_with_index do |value, index|
@comparison_matrix[index][value] += 1
end
end
end
def display
maximum_digits = @runs.to_s.length + 1
expected_average = (@runs / @array.size.to_f).round(2)
puts "#{@method} - expected average: #{expected_average} (#{@runs} runs)"
@comparison_matrix.each_with_index do |row, i|
row.each_with_index do |value, j|
distance_from_expected_average = (expected_average - value) * -1
output_string = distance_from_expected_average.to_s.gsub('.',',').rjust(maximum_digits)
print("#{output_string};")
end
print "\n"
end
print "\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment