Random value from IEnumerable - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Random value from IEnumerable

Demo Code


using System.Reflection;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from   w  w w  .  java2 s  .com*/

public class Main{
  public static T Random<T>(this IEnumerable<T> enumerable)
   {
      var list = enumerable as IList<T> ?? enumerable.ToList();
      return list.ElementAt(UnityEngine.Random.Range(0, list.Count()));
   }
}

Related Tutorials