You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Feel/MMTools/Tools/MMExtensions/MMArrayExtensions.cs

43 lines
1.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MoreMountains.Tools
{
/// <summary>
/// Array extensions
/// </summary>
public static class MMArrayExtensions
{
/// <summary>
/// Returns a random value inside the array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static T MMRandomValue<T>(this T[] array)
{
int newIndex = Random.Range(0, array.Length);
return array[newIndex];
}
/// <summary>
/// Shuffles an array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static T[] MMShuffle<T>(this T[] array)
{
// Fisher Yates shuffle algorithm, see https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
for (int t = 0; t < array.Length; t++)
{
T tmp = array[t];
int randomIndex = Random.Range(t, array.Length);
array[t] = array[randomIndex];
array[randomIndex] = tmp;
}
return array;
}
}
}