using System; using System.Collections.Generic; using System.Linq; namespace MLEM.Extensions { public static class RandomExtensions { public static T GetRandomEntry(this Random random, params T[] entries) { return entries[random.Next(entries.Length)]; } public static T GetRandomEntry(this Random random, IList entries) { return entries[random.Next(entries.Count)]; } public static T GetRandomWeightedEntry(this Random random, IList entries, Func weightFunc) { var totalWeight = entries.Sum(weightFunc); var goalWeight = random.Next(totalWeight); var currWeight = 0; foreach (var entry in entries) { currWeight += weightFunc(entry); if (currWeight >= goalWeight) return entry; } throw new IndexOutOfRangeException(); } } }