Skip to content

Instantly share code, notes, and snippets.

@briandoll
Created March 19, 2010 21:20
Show Gist options
  • Save briandoll/338200 to your computer and use it in GitHub Desktop.
Save briandoll/338200 to your computer and use it in GitHub Desktop.
# is this not already provided somewhere?
# even_distribution?([50,49,53]) => true
# even_distribution?(50,33,70]) => false
def even_distribution?(array, slack = 0.1)
exact_distribution = 1 / array.size.to_f
slack_val = exact_distribution * slack
ceil = exact_distribution + slack_val
floor = exact_distribution - slack_val
total = array.reduce{ |v,t| v + t }.to_f
array.each do |element|
distribution = element / total
return false unless (floor..ceil).include?(distribution)
end
true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment