Get Random value from IEnumerable - CSharp System.Collections

CSharp examples for System.Collections:IEnumerable

Description

Get Random value from IEnumerable

Demo Code


using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;// w w w .  java 2 s.  c o  m

public class Main{
        public static T GetRandom<T>(this IEnumerable<T> array)
        {
            //Determine the max length of our incoming array
            var maxLength = array.Count();

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

Related Tutorials