Get Random Sub Selection from IEnumerable - CSharp System.Collections

CSharp examples for System.Collections:IEnumerable

Description

Get Random Sub Selection from IEnumerable

Demo Code


using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;//w w w  . j a v a2 s  . c o  m

public class Main{
        public static T GetRandomSubSelection<T>(this IEnumerable<T> array, int start, int count)
        {
            //Get a sub-selection of the current array if the parameters are valid...
            var subArray = array.ToList().GetRange(start, count);

            //Determine the max length of our incoming array
            var maxLength = subArray.Count;

            return subArray.ElementAt(R.Next(0, maxLength));
        }
}

Related Tutorials