Created
March 19, 2010 21:20
-
-
Save briandoll/338200 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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