Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Created July 31, 2012 21:57
Show Gist options
  • Save bil-bas/3221023 to your computer and use it in GitHub Desktop.
Save bil-bas/3221023 to your computer and use it in GitHub Desktop.
Random hash picker
heftig:
first, do target = rand(a.inject(0){|m,h| m + h[:prob]})
to get a value between 0 and (sum of all probs - 1)
then just a.find { |h| target -= h[:prob]; target < 0 }
steps through the array, reducing target by prob until it's < 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment