Skip to content

Instantly share code, notes, and snippets.

@shiffman
Created May 1, 2017 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiffman/f71672bad4e8f503db0ea98e4f2270b0 to your computer and use it in GitHub Desktop.
Save shiffman/f71672bad4e8f503db0ea98e4f2270b0 to your computer and use it in GitHub Desktop.
Pool Selection
function pickOne(list) {
var index = -1;
var r = random(1);
while (r > 0) {
index++;
r = r - list[index].prob;
}
return list[index];
}
@akseidel
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment