Skip to content

Instantly share code, notes, and snippets.

@djburdick
Created February 4, 2012 23:56
Show Gist options
  • Save djburdick/1741223 to your computer and use it in GitHub Desktop.
Save djburdick/1741223 to your computer and use it in GitHub Desktop.
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
@jerhinesmith
Copy link

I don't think you want to be re-calculating that random value every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment