Created
February 4, 2012 23:56
-
-
Save djburdick/1741223 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
MUL = 100 # normalize to whole number | |
def random_item | |
items = items.merge!('' => (100 - items.values.sum.to_f)).sort_by { |k,v| v } | |
percentile = 0 | |
items.each do |loot,prob| | |
percentile += prob | |
return loot if rand(100*MUL) < (percentile*MUL) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't think you want to be re-calculating that random value every time.