Get Random Item - OuterRimStudios/Utilities GitHub Wiki

CollectionUtilities.GetRandomItem

public static T GetRandomItem<T>(this T[] array)

Parameters

array The array of objects to choose from.

Returns

T a random element from the entered array.

Description

Get a random element of a given array.

using UnityEngine;
using OuterRimStudios.Utilities;

public class ExampleClass : MonoBehaviour
{
    string[] names = new string[] {"Sam", "Hector", "Jeff"};

    void Start()
    {
        string randName = CollectionUtilites.GetRandomItem(names);    //Returns a random name from the array
    }
}

public static T GetRandomItem<T>(this List<T> list)

Parameters

list The list of objects to choose from.

Returns

T a random element from the entered list.

Description

Get a random element of a given list.

using UnityEngine;
using OuterRimStudios.Utilities;

public class ExampleClass : MonoBehaviour
{
    List<string> names = new List<string>() {"Sam", "Hector", "Jeff"};

    void Start()
    {
        string randName = CollectionUtilites.GetRandomItem(names);    //Returns a random name from the array
    }
}
⚠️ **GitHub.com Fallback** ⚠️