Created
April 17, 2020 12:50
-
-
Save z-------------/38c9043d5d470083a4db80e7fdbb7eae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def roll(probs) | |
| r = rand | |
| cum = 0 | |
| outcome = probs.keys.first | |
| outcome_prob = 0 | |
| probs.each do |key, prob| | |
| cum += prob | |
| if cum >= r | |
| outcome = key | |
| outcome_prob = prob | |
| break | |
| end | |
| end | |
| return outcome, outcome_prob | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment