Get Random Items - OuterRimStudios/Utilities GitHub Wiki

CollectionUtilities.GetRandomItems

public static List<T> GetRandomItems(this List<T> list, int amount)

Parameters

list The list of objects to choose from.

amount The number of items in the returned list.

Returns

List<T> Returns a randomized list of elements from the entered list.

Description

Gets a list of random elements of a given list.

using UnityEngine;
using OuterRimStudios.Utilities;

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

    void Start()
    {
        List<string> randName = CollectionUtilites.GetRandomItems(names, 2);    //Returns a random list of names from the list.
    }
}

public static T[] GetRandomItems<T>(this T[] array, int amount)

Parameters

array The array of objects to choose from. amount The number of items in the returned array.

Returns

Returns a randomized array of elements from the entered array.

Description

Gets an array of random elements 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.GetRandomItems(names, 2);    //Returns a random list of names from the list.
    }
}
⚠️ **GitHub.com Fallback** ⚠️