Skip to content

Instantly share code, notes, and snippets.

@TWolverson
Created October 19, 2014 12:54
Show Gist options
  • Save TWolverson/70c2a5a722573e166603 to your computer and use it in GitHub Desktop.
Save TWolverson/70c2a5a722573e166603 to your computer and use it in GitHub Desktop.
var sample = random.NextDouble(1);
var cumulative = 0.0;
var selectedItem = items.TakeWhile(item => {
cumulative += item.Probability; // probability is a ratio i.e. fraction of 1
return cumulative <= sample;
}).Last();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment