Skip to content

Instantly share code, notes, and snippets.

@z-------------
Created April 17, 2020 12:50
Show Gist options
  • Save z-------------/38c9043d5d470083a4db80e7fdbb7eae to your computer and use it in GitHub Desktop.
Save z-------------/38c9043d5d470083a4db80e7fdbb7eae to your computer and use it in GitHub Desktop.
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