Skip to content

Instantly share code, notes, and snippets.

@riceluxs1t
Created February 9, 2016 07:30
Show Gist options
  • Save riceluxs1t/a9790254470aeb3d6874 to your computer and use it in GitHub Desktop.
Save riceluxs1t/a9790254470aeb3d6874 to your computer and use it in GitHub Desktop.
func throw_dice (arr_weights) {
W <- sum(arr_weights)
T <- RANDOM(0, W) // sample uniformly from [0, W)
cumsum <- 0
FOR i in 0..n-1, DO {
IF T-cumsum <= arr_weigths[i], THEN
RETURN arr_weights[i]
cumsum += arr_weights[i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment