mirror of
https://github.com/ConjureETS/Unity_Utils.git
synced 2026-03-23 20:40:58 +00:00
26 lines
906 B
C#
26 lines
906 B
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace Utilities.Extensions
|
|
{
|
|
public static class CollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// Returns a random element from the enumerable.
|
|
/// </summary>
|
|
/// <param name="thisIEnumerable">The enumerable to return a random element from.</param>
|
|
/// <typeparam name="T">The type of the enumerable.</typeparam>
|
|
/// <returns>A random element from the list, or the default type value if the list is empty.</returns>
|
|
public static T RandomElement<T>(this IEnumerable<T> thisIEnumerable)
|
|
{
|
|
T[] thisArray = thisIEnumerable as T[] ?? thisIEnumerable.ToArray();
|
|
|
|
if (!thisArray.Any())
|
|
return default;
|
|
|
|
int randomIndex = Random.Range(0, thisArray.Length);
|
|
return thisArray[randomIndex];
|
|
}
|
|
}
|
|
} |