using System.Collections.Generic; using System.Linq; using UnityEngine; namespace JohnsonUtils.Utilities { public static class CollectionExtensions { /// /// Returns a random element from the enumerable. /// /// The enumerable to return a random element from. /// The type of the enumerable. /// A random element from the list, or the default type value if the list is empty. public static T RandomElement(this IEnumerable thisIEnumerable) { T[] thisArray = thisIEnumerable as T[] ?? thisIEnumerable.ToArray(); if (!thisArray.Any()) return default; int randomIndex = Random.Range(0, thisArray.Length); return thisArray[randomIndex]; } } }