示例#1
0
        public static T ChooseRange <T>(T[] list, int min, int max) where T : IWeighted
        {
            //Grab a random value right quick, make sure the list is working and all
            T choice = Weighted.Choose(list);

            //4096 changes to pick a number from the range.
            //If it takes longer than that something isn't right.
            for (int i = 0; i < 4096; i++)
            {
                choice = Choose(list);
                if (choice.Value is int)
                {
                    int x = (int)choice.Value;
                    if (x >= min && x <= max)
                    {
                        return(choice);
                    }
                }
            }

            if (choice is WeightedGeneric <int> )
            {
                int w = choice.Weight;
                choice       = Activator.CreateInstance <T>();
                choice.Value = min < max?BetterRng.Twister.Next(min, max) : BetterRng.Twister.Next(Math.Max(min, max));

                choice.Weight = w;
                return(choice);
            }

            throw new ArgumentOutOfRangeException(nameof(min) + nameof(max), "The requested value was not in the Min/Max range supplied.");
        }
示例#2
0
 public static T Choose <T>(this List <T> list) where T : IWeighted
 {
     return(Weighted.Choose(list));
 }