Created
May 1, 2017 20:43
-
-
Save shiffman/f71672bad4e8f503db0ea98e4f2270b0 to your computer and use it in GitHub Desktop.
Pool Selection
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
function pickOne(list) { | |
var index = -1; | |
var r = random(1); | |
while (r > 0) { | |
index++; | |
r = r - list[index].prob; | |
} | |
return list[index]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am not sure pickOne(list) does what it is supposed to do when the list is not sorted by list[].prob.
Consider the list 0|.10, 1|.60, 2|.20, 3|.10 where we have four items 0 to 3 having prob. .10, .60, .20 and .10 in that order. The function pickOne(list) will select item 0 when R = .10 or less. The function will select item 1 for any R > .10.
AKS