Skip to content

Instantly share code, notes, and snippets.

@knzconnor
Created April 3, 2009 18:36
Show Gist options
  • Save knzconnor/89887 to your computer and use it in GitHub Desktop.
Save knzconnor/89887 to your computer and use it in GitHub Desktop.
def assign_by_weight(weights)
sum = weights.keys.inject(0) { |s,v| s += v }
#normalize random over sum
#parse weights into some form of mutually exclusive set
return result
end
foo = assign_by_weight(0.5 => :foo, 0.6 => :bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment