Created
July 31, 2012 21:57
-
-
Save bil-bas/3221023 to your computer and use it in GitHub Desktop.
Random hash picker
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
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