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;
}