Пример #1
0
        public static T PickWeighted <T>(this IEnumerable <T> collection, RNG rng, double totalWeight, Func <T, double> weightSelectionFunc, T defaultValue = default(T))
        {
            double selection = rng.NextDouble(totalWeight);

            foreach (T t in collection)
            {
                if (selection < weightSelectionFunc(t))
                {
                    return(t);
                }
                selection -= weightSelectionFunc(t);
            }
            return(defaultValue);
        }