using System.Collections.Generic;
public class WeightedItem<T>
private int cumulativeSum;
private static Random rndInst = new Random();
public WeightedItem(T value, int weight)
public static T Choose(List<WeightedItem<T>> items)
for (int slot = 0; slot < cnt; slot++)
cumulSum += items[slot].weight;
items[slot].cumulativeSum = cumulSum;
double divSpot = rndInst.NextDouble() * cumulSum;
WeightedItem<T> chosen = items.FirstOrDefault(i => i.cumulativeSum >= divSpot);
if (chosen == null) throw new Exception("No item chosen - there seems to be a problem with the probability distribution.");
WeightedItem<string> alice = new WeightedItem<string>("alice", 1);
WeightedItem<string> bob = new WeightedItem<string>("bob", 1);
WeightedItem<string> charlie = new WeightedItem<string>("charlie", 1);
WeightedItem<string> diana = new WeightedItem<string>("diana", 4);
WeightedItem<string> elaine = new WeightedItem<string>("elaine", 1);
List<WeightedItem<string>> myList = new List<WeightedItem<string>> { alice, bob, charlie, diana, elaine };
string chosen = WeightedItem<string>.Choose(myList);