Pchp.Library.Arrays.RandomSubset C# (CSharp) Method

RandomSubset() private static method

Chooses specified number of items from a collection at random.
Items are chosen uniformly in time O(n), where n is the number of items in the collection using conveyor belt sampling.
Either or or /// is a null reference (Warning) is not positive and less /// than the number of items in . (Warning)
private static RandomSubset ( ICollection source, IList result, int count, Random generator ) : bool
source ICollection The from which to choose.
result IList The where to add chosen items.
count int The number of items to choose.
generator System.Random The initialized random numbers generator.
return bool
        private static bool RandomSubset(ICollection<IntStringKey> source, IList<PhpValue> result, int count, Random generator)
        {
            if (source == null)
            {
                // TODO: PhpException.ArgumentNull("array");
                return false;
            }
            if (result == null)
            {
                // TODO: PhpException.ArgumentNull("result");
                return false;
            }
            if (generator == null)
            {
                // TODO: PhpException.ArgumentNull("generator");
                return false;
            }
            if (count < 1 || count > source.Count)
            {
                // TODO: PhpException.InvalidArgument("count", LibResources.GetString("number_of_items_not_between_one_and_item_count", count, source.Count));
                return false;
            }

            int n = source.Count;
            using (var iterator = source.GetEnumerator())
            {
                while (iterator.MoveNext())
                {
                    // adds item to result with probability count/n:
                    if ((double)count > generator.NextDouble() * n)
                    {
                        result.Add(PhpValue.Create(iterator.Current));
                        if (--count == 0) break;
                    }
                    n--;
                }
            }

            return true;
        }