예제 #1
0
        public static void ForRandomByOnce <T>(this IEnumerable <T> set, int count, Action <T> action)
        {
            var setCount = set.Count();

            if (count >= setCount)
            {
                throw new Exception($"Selection count {count} more than item count {setCount}!");
            }

            var list    = new System.Collections.Generic.List <T>(set);
            var pointer = Int.ZERO;

            while (pointer < count)
            {
                var item = list.GetRandom();
                action.Invoke(item);
                list.Remove(item);
                pointer++;
            }
        }
예제 #2
0
        public static T GetRandomWhile <T>(
            this IEnumerable <T> set,
            out bool isFound,
            Func <T, bool> predicate
            )
        {
            var list = new System.Collections.Generic.List <T>(set);

            while (list.IsNotEmpty())
            {
                var item = list.GetRandom();
                if (predicate(item))
                {
                    isFound = true;
                    return(item);
                }

                list.Remove(item);
            }

            isFound = false;
            return(default(T));
        }